﻿

###
#	SPECIAL NOTE
#
#	When evaluating whether or not the AI can send this interaction to the
#	player, the AI will use the ai_accept value and send a proposal to the 
#	player IF AND ONLY IF the player, as an AI actor, would have positive
#	ai_accept value. This is a special case for this specific interaction, 
#	and is being calculated specifically in code.  
#
arrange_marriage_interaction = {
	icon = icon_marriage
	category = interaction_category_diplomacy
	common_interaction = yes

	override_background = { reference = relaxing_room }

	special_interaction = arrange_marriage_interaction
	interface = marriage
	use_diplomatic_range = {
		trigger_if = {
			limit = { exists = scope:secondary_actor.betrothed }
			NOT = {
				scope:secondary_actor = {
					betrothed = scope:secondary_recipient
				}
			}
		}
	} 

	recipient_recieve_cooldown = {
		years = marriage_recieve_proposal_cooldown
	}

	ignore_recipient_recieve_cooldown = {
		AND = {
			is_ai = no
			scope:recipient = { is_ai = no }
		}
	}

	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:actor = scope:recipient
				}
				desc = arrange_marriage_interaction_desc_ruler_at_own_court
			}
			triggered_desc = {
				trigger = {
					scope:actor = {
						can_marry_character_trigger = {
							CHARACTER = scope:recipient
						}
					}
					scope:recipient = { is_ruler = yes }
				}
				desc = arrange_marriage_interaction_desc_both_rulers_can_marry
			}
			triggered_desc = {
				trigger = {
					scope:recipient = {
						is_ruler = yes
						can_marry_trigger = yes
					}
				}
				desc = arrange_marriage_interaction_desc_both_rulers_recipient_only_can_marry
			}
			triggered_desc = {
				trigger = {
					scope:recipient = {
						is_ruler = yes
					}
				}
				desc = arrange_marriage_interaction_desc_both_rulers_recipient_neither_can_marry
			}
			triggered_desc = {
				trigger = {
					scope:actor = {
						can_marry_character_trigger = {
							CHARACTER = scope:recipient
						}
					}
				}
				desc = arrange_marriage_interaction_desc_one_ruler_can_marry
			}
			desc = arrange_marriage_interaction_desc_recipient_one_ruler_cannot_marry
		}
	}
	
	redirect = {
		# If the define MARRIAGE_OWNER_IS_MATCHMAKER is set to yes, the actor must end up as the "matchmaker" link for courtiers
		scope:actor = {
			if = {
				limit = {
					exists = matchmaker
					NOT = { this = matchmaker }
				}
				save_scope_as = secondary_actor
				matchmaker = {
					save_scope_as = actor
				}
			}
			if = {
				limit = {
					is_betrothed = yes
					betrothed = scope:recipient
				}

				save_scope_as = secondary_actor
			}
		}

		scope:recipient = {
			if = {
				limit = {
					is_betrothed = yes
					betrothed.matchmaker = scope:actor
					is_adult = yes
					betrothed = {
						is_adult = yes
					}
				}

				save_scope_as = secondary_recipient
				matchmaker = {
					save_scope_as = recipient
				}
				betrothed = {
					save_scope_as = secondary_actor
				}
			}
			if = {
				limit = {
					exists = matchmaker
					NOT = { this = matchmaker }
				}
				save_scope_as = secondary_recipient
				matchmaker = {
					save_scope_as = recipient
				}
			}
		}
	}

	populate_actor_list = {
		scope:actor = {
			add_to_list = characters
			every_courtier = {
				limit = {
					trigger_if = {
						limit = {
							is_concubine = yes
						}
						is_concubine_of = scope:actor
						can_marry_character_trigger = { CHARACTER = scope:actor }
					}
				}
				add_to_list = characters
			}
			every_courtier_away = {
				limit = {
					trigger_if = {
						limit = {
							is_concubine = yes
						}
						is_concubine_of = scope:actor
						can_marry_character_trigger = { CHARACTER = scope:actor }
					}
				}
				add_to_list = characters
			}
			every_child = {
				even_if_dead = yes
				if = {
					limit = {
						is_alive = yes
						NOT = { is_in_list = characters }
						trigger_if = {
							limit = {
								is_concubine = yes
							}
							is_concubine_of = scope:actor
							can_marry_character_trigger = { CHARACTER = scope:actor }
						}
					}
					add_to_list = characters
				}
				every_child = {
					even_if_dead = yes
					if = {
						limit = {
							is_alive = yes
							NOT = { is_in_list = characters }
							OR = {
								top_liege = scope:actor
								house.house_head = scope:actor
								dynasty.dynast = scope:actor
							}
							trigger_if = {
								limit = {
									is_concubine = yes
								}
								is_concubine_of = scope:actor
								can_marry_character_trigger = { CHARACTER = scope:actor }
							}
						}
						add_to_list = characters
					}
					every_child = {
						if = {
							limit = {
								is_alive = yes
								NOT = { is_in_list = characters }
								OR = {
									top_liege = scope:actor
									house.house_head = scope:actor
									dynasty.dynast = scope:actor
								}
								trigger_if = {
									limit = {
										is_concubine = yes
									}
									is_concubine_of = scope:actor
									can_marry_character_trigger = { CHARACTER = scope:actor }
								}
							}
							add_to_list = characters
						}
					}
				}
			}
		}
	}

	populate_recipient_list = {
		scope:recipient = {
			add_to_list = characters
			every_courtier = {
				add_to_list = characters
			}
			every_courtier_away = {
				add_to_list = characters
			}
			every_child = {
				limit = {
					NOT = { is_in_list = characters }
				}
				add_to_list = characters
			}
		}
	}

	interface_priority = 60

	cost = {
		prestige = {
			value = 0
			if = {
				limit = {
					exists = scope:actor
					exists = scope:recipient
					exists = scope:secondary_recipient
					exists = scope:secondary_actor
					scope:secondary_actor = { is_close_family_of = scope:actor }
					scope:recipient = {
						OR = {
							AND = {
								vassal_contract_has_flag = vassal_contract_marriage_favor
								is_allied_to = scope:actor
							}
							NOT = { vassal_contract_has_flag = vassal_contract_marriage_favor}
						}
					}
				}
				add = {
					scope:actor = {
						every_vassal = {
							limit = {
								vassal_contract_has_flag = vassal_contract_marriage_favor
								NOT = { is_allied_to = scope:actor }
								NOR = {
									this = scope:recipient
									this = scope:secondary_recipient
								}
							}
							add = medium_prestige_value
						}
					}
					desc = VASSAL_WITH_MARRIAGE_FAVOR
				}
			}
		}

		piety = {
			value = 0
			if = {
				limit = { scope:piety_cost_reduction = yes }
				add = scope:actor.medium_piety_value
				desc = STRUGGLE_MARRIAGE_ACCEPTANCE_USING_PIETY_AS_CURRENCY
			}
		}

		influence = {
			value = 0
			if = {
				limit = { scope:influence_send_option = yes }
				add = scope:actor.medium_influence_value
				desc = INFLUENCE_INTERACTION_ACCEPTANCE_SEND_OPTION
			}
		}
	}

	# actor, recipient ... characters arranging the marriage
	# secondary_actor, secondary_recipient ... characters that should marry

	is_shown = {
		scope:actor = {
			OR = {
				is_ruler = yes #Can arrange marriages for others
				#You're just a courtier? Only use the interaction on characters you can marry
				can_marry_character_trigger = { CHARACTER = scope:recipient }
			}
		}
		# Block further marriage proposals for people who have a pending grand wedding
		NOR = {
			scope:secondary_actor ?= { has_been_promised_grand_wedding = yes }
			scope:secondary_recipient ?= { has_been_promised_grand_wedding = yes }
		}
		#If you're interacting with someone who isn't their own matchmaker, only show the interaction if they can get married. (If someone isn't their own matchmaker, they will be set as secondary_recipient by code and their matchmaker becomes recipient)
		trigger_if = {
			limit = { exists = scope:secondary_recipient }
			scope:secondary_recipient = {
				OR = {
					marriage_interaction_can_be_picked_trigger = yes #Can marry anyone
					AND = {
						ready_to_marry_betrothed_trigger = yes
						OR = {
							betrothed = scope:secondary_actor
							trigger_if = {
								limit = { exists = betrothed.liege }
								betrothed.liege = scope:actor
							}
						}
					}
				}
				#To account for a person marrying their concubines
				trigger_if = {
					limit = {
						is_concubine = yes
					}
					is_concubine_of = scope:actor
					can_marry_character_trigger = { CHARACTER = scope:actor }
				}
			}
		}
		#allow landless adventurers to marry only within their camp, or with Contact list courtiers (after using request_marriage option in contact_list_request_interaction), or prieviously betrothed characters
		trigger_if = {
			limit = {
				scope:actor = {
					is_landless_adventurer = yes
				}
			}
			trigger_if = {
				limit = {
					OR = {
						scope:actor = {
							is_target_in_variable_list = {
								name = contact_list_marriage
								target = scope:recipient
							}
						}
						scope:recipient = {
							is_betrothed = yes
							betrothed = {
								OR = {
									scope:actor = this
									scope:secondary_actor ?= this
								}
							}
						}
						scope:secondary_recipient ?= {
							is_betrothed = yes
							betrothed = {
								OR = {
									scope:actor = this
									scope:secondary_actor ?= this
								}
							}
						}
					}
				}
				always = yes
			}
			trigger_else = {
				scope:recipient = scope:actor
			}
		}
	}
	
	is_valid_showing_failures_only = {
		#If we're opening this interaction through the interaction menu (i.e. not all scopes exist yet)
		#This was giving double-warnings about a marry-able character existing so it's commented out for now
		#custom_description = {
		#	text = character_to_marry_exists
		#	object = scope:recipient
		#	scope:recipient = {
		#		OR = {
		#			any_courtier = {
		#				is_at_home = yes
		#				marriage_interaction_can_be_picked_trigger= yes
		#			}
		#			any_child = {
		#				marriage_interaction_can_be_picked_trigger= yes
		#			}
		#		}
		#	}
		#}
		trigger_if = {
			limit = {
				NAND = {
					exists = scope:secondary_actor
					exists = scope:secondary_recipient
				}
			}
			#There is an available marriage
			custom_description = {
				text = "basic_can_marry_check_recipient"
				subject = scope:actor
				object = scope:recipient
				scope:recipient = {
					OR = {
						AND = {
							#Recipient can marry
							can_marry_trigger = yes
							#To account for a person marrying their concubines
							trigger_if = {
								limit = {
									is_concubine = yes
								}
								is_concubine_of = scope:actor
								can_marry_character_trigger = { CHARACTER = scope:actor }
							}
						}
						AND = {
							is_ruler = yes #(Following are only for rulers)
							OR = {
								any_child = {
									matchmaker = scope:actor
									OR = {
										#Any of recipient's children can marry
										AND = {
											can_marry_trigger = yes
											#To account for a person marrying their concubines
											trigger_if = {
												limit = {
													is_concubine = yes
												}
												is_concubine_of = scope:actor
											}
										}
										#Any of recipient's children are ready to marry their betrothed
										AND = {
											ready_to_marry_betrothed_trigger = yes
											betrothed.matchmaker = scope:actor
										}
									}
								}
								any_courtier = {
									OR = {
										#Any of recipient's courtiers can marry
										AND = {
											can_marry_trigger = yes
											#To account for a person marrying their concubines
											trigger_if = {
												limit = {
													is_concubine = yes
												}
												is_concubine_of = scope:actor
											}
										}
										#Any of recipient's courtiers are ready to marry their betrothed
										AND = {
											ready_to_marry_betrothed_trigger = yes
											betrothed.matchmaker = scope:actor
										}
									}
								}
							}
						}
					}
				}
			}
		}
		#Diplomatic availability & prison
		scope:recipient = {
			is_busy_in_events_localised = yes
			is_imprisoned = no
		}
		NOT = {
			scope:actor = {
				is_at_war_with = scope:recipient
			}
		}
		trigger_if = { #(must be here because secondary_recipient can be set at the start)
			limit = {
				exists = scope:secondary_recipient
				NOT = { scope:secondary_recipient = scope:recipient }
			}
			scope:secondary_recipient = {
				is_busy_in_events_localised = yes
				is_imprisoned = no
				bp2_valid_for_standard_interactions_trigger = yes
			}
		}
		trigger_if = { #(must be here because secondary_recipient can be set at the start)
			limit = {
				exists = scope:secondary_actor
				NOT = { scope:secondary_actor = scope:actor }
			}
			scope:secondary_actor = {
				is_busy_in_events_localised = yes
				is_imprisoned = no
				bp2_valid_for_standard_interactions_trigger = yes
			}
		}
		# Account for regents being bolshy.
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = secondary_actor }
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = secondary_recipient }
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = actor }
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = recipient }

		# Restrict marriageable range
		custom_description = {
			trigger_if = {
				limit = {
					OR = {
						NOT = { exists = scope:secondary_actor.betrothed }
						AND = {
							exists = scope:secondary_actor.betrothed
							NOT = {
								scope:secondary_actor = {
									betrothed = scope:secondary_recipient
								}
							}
						}
					}
				}
				OR = {
					AND = {
						exists = scope:recipient.capital_county
						scope:actor = {
							realm_to_title_distance_squared = {
								title = scope:recipient.capital_county
								value <= 1000000	# Square of 1000
							}
						}
					}
					AND = {
						NOT = { exists = scope:recipient.capital_county }
						exists = scope:recipient.location.county
						scope:actor = {
							realm_to_title_distance_squared = {
								title = scope:recipient.location.county
								value <= 1000000	# Square of 1000
							}
						}
					}
				}
			}
			text = shogunate_not_in_diplomatic_range_trigger
		}
	}
	
	# whether a character can be picked for the interaction, interface only (e.g. picking the first character in the marriage interaction)
	# in character scope, actor and recipient event targets can be used, but not the secondary characters (because none exist yet)
	can_be_picked = {
		OR = {
			marriage_interaction_can_be_picked_trigger = yes
			AND = {
				exists = scope:secondary_actor
				exists = scope:secondary_recipient
				scope:secondary_actor.betrothed = scope:secondary_recipient
			}
		}
	}

	#Is evaluated only after secondary_recipient and secondary_actor are both chosen
	has_valid_target_showing_failures_only = {
		marriage_interaction_valid_target_trigger = yes  #Checks marriage status, betrothed, gender, consanguinity, faith hostility etc.

		#Blocks AI from marrying player guests
		scope:secondary_recipient = {
			trigger_if = {
				limit = {
					is_pool_guest = yes
					host = {
						is_ai = no
						NOT = { this = scope:actor }
					}
				}
				NOT = { is_pool_guest_of = host }
			}
		}
		
		#Blocks most AI's from marrying Evil faiths
		trigger_if = {
			limit = {
				scope:actor = {
					is_ai = yes
					NOT = { has_trait = cynical }
					#AI should honor betrothal even if faiths change
					NOT = { scope:secondary_actor.betrothed = scope:secondary_recipient }
				}
			}
			scope:recipient.faith = {
				faith_hostility_level = {
					target = scope:actor.faith
					value < faith_hostility_prevents_marriage_level
				}
			}
		}

		#Blocks AI from marrying single mayors in their area unless they are also mayors
		trigger_if = {
			limit = {
				scope:actor = {
					is_ai = yes
					NOT = { government_has_flag = government_is_republic }
				}
			}
			NOT = {
				scope:recipient = { government_has_flag = government_is_republic }
			}
		}
		
		trigger_if = { # Cultural Traditions, for AI
			limit = {
				scope:actor = {
					is_ai = yes
					culture = {
						has_cultural_parameter = dislikes_marrying_outside_of_culture
					}
				}
			}
			OR = {
				scope:actor.culture = scope:recipient.culture
				scope:actor.culture = scope:secondary_recipient.culture
			}
		}
	}

	pre_auto_accept = {
		if = {
			limit = {
				NOR = {
					scope:recipient = scope:actor
					scope:secondary_actor = { has_been_promised_grand_wedding = yes }
					scope:secondary_recipient = { has_been_promised_grand_wedding = yes }
				}
			}
			scope:recipient = {
				if = {
					limit = { scope:secondary_actor.betrothed = scope:secondary_recipient }
					send_interface_toast = {
						type = msg_betrothal_fulfilled
						left_icon = scope:secondary_actor
						right_icon = scope:secondary_recipient
					}
				}
				else = {
					send_interface_toast = {
						type = msg_marriage_forced
						left_icon = scope:secondary_actor
						right_icon = scope:secondary_recipient
					}
				}
			}
		}
	}

	on_send = {
		if = {
			limit = {
				scope:grand_wedding_promise = yes
			}
			scope:actor = { 
				set_variable = {
					name = grand_wedding_promise_pending
					days = 10
				}
				if = {
					limit = {
						is_ai = yes
					}
					remove_short_term_gold = ai_wedding_promise_gold_actor
				}
				#if = { # Activity count debugging, left in for QA purposes
				#	limit = {
				#		NOT = {
				#			exists = global_var:wedding_promise
				#		}
				#	}
				#	set_global_variable = {
				#		name = wedding_promise
				#		value = 1
				#	}
				#}
				#else = {
				#	change_global_variable = {
				#		name = wedding_promise
				#		add = 1
				#	}
				#}
			}
		}
	}

	on_accept = {
		marriage_interaction_on_accept_effect = yes
		scope:actor = {
			if = {
				limit = {
					scope:herd_send_option = yes
				}
				pay_herd = {
					target = scope:recipient
					value = scope:actor.domicile.medium_herd_value
				}
			}
			if = {
				limit = {
					has_variable = grand_wedding_promise_pending
				}
				remove_variable = grand_wedding_promise_pending
			}
			if = {
				limit = {
					is_target_in_variable_list = {
						name = contact_list_marriage
						target = scope:recipient
					}
				}
				remove_list_variable = {
					name = contact_list_marriage
					target = scope:recipient
				}
			}
		}
	}

	on_decline = {
		scope:actor = {
			if = {
				limit = { NOT = { this = scope:recipient } }
				trigger_event = marriage_interaction.0011	
			}
			if = {
				limit = {
					has_variable = grand_wedding_promise_pending
				}
				remove_variable = grand_wedding_promise_pending
			}
		}
		scope:secondary_actor = {
			add_character_flag = {
				flag = player_declined_marriage
				years = 5
			}
		}
	}

	auto_accept = {
		marriage_interaction_auto_accept_trigger = yes #Strong hook conditions
	}

	ai_accept = {
		base = 0

		marriage_ai_accept_modifier = yes
	}

	# If you want the AI to set these options then it needs to be hooked into the code as well
	send_option = {
		flag = grand_wedding_promise
		localization = GRAND_WEDDING_PROMISE

		is_shown = {
			scope:actor = { #Can organize a Grand Wedding
				highest_held_title_tier > tier_barony
				is_landed_or_landless_administrative = yes
				trigger_if = {
					limit = {
						is_ai = yes
					}
					is_at_war = no
				}
				# DLC locked
				has_dlc_feature = tours_and_tournaments
				has_dlc_feature = advanced_activities
			}
		}

		current_description = {
			desc = "GRAND_WEDDING_PROMISE_INFO"
		}

		is_valid = {
			scope:actor = {
				gold >= real_wedding_cost_actor_scope
				is_available = yes
				custom_tooltip = {
					text = grand_wedding_already_promised_tt
					NOR = {
						AND = {
							has_variable = promised_grand_wedding_marriage_countdown
							var:promised_grand_wedding_marriage_countdown = { is_alive = yes }
						}
						has_variable = grand_wedding_promise_pending
					}
				}
			}
			trigger_if = {
				limit = {
					exists = scope:secondary_actor
				}
				custom_tooltip = {
					text = grand_wedding_only_dominant_side
					trigger_if = {
						limit = {
							scope:secondary_actor ?= {
								is_female = yes
							}
						}
						OR = {
							scope:secondary_actor ?= { matrilinear_betrothal = yes }
							scope:matrilineal = yes
						}
					}
					trigger_else = {
						NOR = {
							scope:secondary_actor ?= { matrilinear_betrothal = yes }
							scope:matrilineal = yes
						}
					}
				}
				custom_tooltip = {
					text = grand_wedding_only_family
					scope:secondary_actor ?= {
						is_close_or_extended_family_of = scope:actor
					}
				}
				# Cannot promise grand weddings with regents due to regency and travel limitations
				custom_tooltip = {
					text = grand_wedding_secondary_regent
					NOT = {
						scope:secondary_actor.liege = {
							diarch = scope:secondary_actor
							has_active_diarchy = yes
						}
					}
				}
			}
			# Safety check because players shouldn't be spouse_2
			trigger_if = {
				limit = {
					has_multiple_players = yes
				}
				custom_tooltip = {
					text = grand_wedding_mp_ai
					trigger_if = {
						limit = {
							OR = {
								scope:actor = { is_ai = no }
								scope:secondary_actor ?= { is_ai = no }
							}
						}
						scope:secondary_recipient ?= {
							is_ai = yes
						}
					}
				}
			}
			trigger_if = {
				limit = {
					exists = scope:secondary_recipient.liege
					scope:secondary_recipient.liege = {
						diarch = scope:secondary_recipient
						has_active_diarchy = yes
					}
				}
				# Cannot promise grand weddings with regents due to regency and travel limitations
				custom_tooltip = {
					text = grand_wedding_secondary_regent
					NOT = {
						scope:secondary_recipient.liege = {
							diarch = scope:secondary_recipient
							has_active_diarchy = yes
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = matrilineal
		localization = MATRILINEAL_OFFER
		is_shown = {
			OR = {
				NOT = { exists = scope:secondary_actor }
				NOT = { exists = scope:secondary_recipient }
				scope:secondary_actor = { sex_opposite_of = scope:secondary_recipient }
			}
		}
		current_description = {
			triggered_desc = {
				trigger = {
					exists = scope:secondary_actor
					scope:secondary_actor = {
						betrothed = scope:secondary_recipient
						matrilinear_betrothal = yes
					}
				}
				desc = "MATRILINEAL_OFFER_BETROTHAL_INFO"
			}
		}
		can_be_changed = {
			trigger_if = {
				limit = {
					exists = scope:secondary_actor
				}
				OR = {
					NOT = {
						exists = scope:secondary_recipient
					}
					scope:secondary_actor = {
						NOT = {
							betrothed = scope:secondary_recipient
						}
					}
				}
			}
		}
		starts_enabled = {
			trigger_if = {
				limit = {
					exists = scope:secondary_actor
					scope:secondary_actor = {
						betrothed = scope:secondary_recipient
					}
				}
				scope:secondary_actor = {
					matrilinear_betrothal = yes
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:secondary_actor
					scope:actor = scope:secondary_actor
					is_female = yes
					is_ai = no
				}
				always = yes
			}
			trigger_else = {
				scope:actor = {
					OR= {
						faith = {
							has_doctrine = doctrine_gender_female_dominated
						}
						AND = {
							this = scope:recipient
							exists = scope:secondary_recipient
							scope:secondary_recipient = {
								is_female = yes
							}
							faith = { has_doctrine = doctrine_gender_equal }
						}
						AND = {
							exists = scope:secondary_actor
							scope:actor = scope:secondary_actor
							is_female = yes
							is_ai = no
						}
					}
				}
			}
		}
	}	

	send_option = {
		is_shown = {
			scope:secondary_actor = {
				NOT = {
					betrothed = scope:secondary_recipient
				}
			}
		}
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = MARRIAGE_HOOK
	}

	send_option = { # FP3
		is_shown = {
			scope:actor = {
				any_character_struggle = {
					is_struggle_type = persian_struggle
					has_struggle_phase_parameter = can_trade_piety_for_marriage_acceptance
				}
			}
		}
		is_valid = {
			scope:actor = {
				any_character_struggle = {
					involvement = involved
					is_struggle_type = persian_struggle
				}
			}
			scope:recipient = {
				any_character_struggle = {
					involvement = involved
					is_struggle_type = persian_struggle
				}
			}
		}
		flag = piety_cost_reduction
		localization = TRADE_PIETY_FOR_BETTER_AI_ACCEPTANCE
	}

	send_option = { # EP3 Influence
		is_shown = { # Actor must have a government that uses influence 
			scope:actor = {
				government_has_flag = government_has_influence
			}
		}
		is_valid = {
			# Actor has enough influence
			scope:actor = { influence >= medium_influence_value }
			# Both characters are within the same top realm
			custom_tooltip = {
				text = not_same_realm_tt
				scope:recipient.top_liege = scope:actor.top_liege
			}
		}
		flag = influence_send_option
		localization = TRADE_INFLUENCE_FOR_BETTER_AI_ACCEPTANCE
	}
	
	send_option = { # MPO Herd
		is_shown = { # Actor must have a government that uses Herd 
			scope:actor = {
				government_has_flag = government_is_nomadic
				is_ai = no
			}
			# Recipient must care about Herd
			scope:recipient = {
				government_has_flag = government_is_nomadic
			}
		}
		is_valid = {
			# Actor has enough Herd
			scope:actor = { domicile.herd >= domicile.medium_herd_value }
		}
		flag = herd_send_option
		localization = TRADE_HERD_FOR_BETTER_AI_ACCEPTANCE
	}

	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"
	
	send_options_exclusive = no
	
	show_effects_in_notification = no
	greeting = positive
	
	notification_text = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:secondary_actor = {
						is_adult = yes
					}
					scope:secondary_recipient = {
						is_adult = yes
					}
					NOR = {
						scope:secondary_actor = { has_been_promised_grand_wedding = yes }
						scope:secondary_recipient = { has_been_promised_grand_wedding = yes }
					}
				}
				desc = {
					first_valid = {	
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_actor = scope:actor
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_BOTH_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_actor = scope:actor
							}
							desc = MARRIAGE_NOTIFICATION_ACTOR_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_RECIPIENT_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
							}
							desc = MARRIAGE_NOTIFICATION_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:secondary_actor = scope:actor
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_BOTH_PERSONAL
						}
						triggered_desc = {
							trigger = {
								scope:secondary_actor = scope:actor
							}
							desc = MARRIAGE_NOTIFICATION_ACTOR_PERSONAL
						}
						triggered_desc = {
							trigger = {
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_RECIPIENT_PERSONAL
						}
						desc = MARRIAGE_NOTIFICATION
					}
				}
			}
			desc = {
				first_valid = {	
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_actor = scope:actor
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_BOTH_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_actor = scope:actor
						}
						desc = BETROTHAL_NOTIFICATION_ACTOR_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_RECIPIENT_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
						}
						desc = BETROTHAL_NOTIFICATION_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:secondary_actor = scope:actor
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_BOTH_PERSONAL
					}
					triggered_desc = {
						trigger = {
							scope:secondary_actor = scope:actor
						}
						desc = BETROTHAL_NOTIFICATION_ACTOR_PERSONAL
					}
					triggered_desc = {
						trigger = {
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_RECIPIENT_PERSONAL
					}
					desc = BETROTHAL_NOTIFICATION
				}
			}
		}
	}
}

#This interaction is referenced in code! If it's renamed you have to ping a coder
marry_off_interaction = {
	icon = icon_marriage
	category = interaction_category_diplomacy
	common_interaction = yes

	desc = marry_off_interaction_desc
	
	send_name = marry_off_send
	needs_recipient_to_open = no
	special_interaction = arrange_marriage_interaction
	interface = marriage
	redirect = {
		if = {
			limit = {
				exists = scope:secondary_actor
			}
			scope:recipient = {
				save_scope_as = secondary_recipient
				matchmaker = {
					save_scope_as = recipient
				}
			}

		}
		else = {
			scope:recipient = {
				save_scope_as = secondary_actor
			}
			clear_saved_scope = recipient
		}
	}

	populate_recipient_list = {
		#populate only with your courtiers or children/grandchildren if you are a landless adventurer
		if = {
			limit = {
				scope:actor = {
					is_landless_adventurer = yes
				}
			}
			scope:secondary_actor = {
				if = {
					limit = {
						NOT = { scope:actor = this }
					}
					add_to_list = characters
				}
				every_courtier = {
					limit = {
						trigger_if = {
							limit = {
								is_concubine = yes
							}
							is_concubine_of = scope:actor
							can_marry_character_trigger = { CHARACTER = scope:actor }
						}
					}
					add_to_list = characters
				}
				every_courtier_away = {
					limit = {
						trigger_if = {
							limit = {
								is_concubine = yes
							}
							is_concubine_of = scope:actor
							can_marry_character_trigger = { CHARACTER = scope:actor }
						}
					}
					add_to_list = characters
				}
				every_child = {
					even_if_dead = yes
					if = {
						limit = {
							is_alive = yes
							NOT = { is_in_list = characters }
							trigger_if = {
								limit = {
									is_concubine = yes
								}
								is_concubine_of = scope:actor
								can_marry_character_trigger = { CHARACTER = scope:actor }
							}
						}
						add_to_list = characters
					}
					every_child = {
						even_if_dead = yes
						if = {
							limit = {
								is_alive = yes
								NOT = { is_in_list = characters }
								trigger_if = {
									limit = {
										is_concubine = yes
									}
									is_concubine_of = scope:actor
									can_marry_character_trigger = { CHARACTER = scope:actor }
								}
							}
							add_to_list = characters
						}
						every_child = {
							if = {
								limit = {
									is_alive = yes
									NOT = { is_in_list = characters }
									trigger_if = {
										limit = {
											is_concubine = yes
										}
										is_concubine_of = scope:actor
										can_marry_character_trigger = { CHARACTER = scope:actor }
									}
								}
								add_to_list = characters
							}
						}
					}
				}
			}
		}
		#otherwise, populate with anyone eligible
		else = {
			scope:secondary_actor = {
				if = {
					limit = {
						allowed_to_marry_same_sex_trigger = yes
					}
					every_opposite_sex_spouse_candidate = {
						add_to_list = characters
					}
					every_same_sex_spouse_candidate = {
						limit = {
							NOT = { scope:secondary_actor = this }
						}
						add_to_list = characters
					}
				}
				else = {
					every_opposite_sex_spouse_candidate = {
						add_to_list = characters
					}
				}
			}
		}
	}

	cost = {
		prestige = {
			value = 0
			if = {
				limit = {
					exists = scope:actor
					exists = scope:recipient
					exists = scope:secondary_recipient
					scope:recipient = {
						OR = {
							AND = {
								vassal_contract_has_flag = vassal_contract_marriage_favor
								is_allied_to = scope:actor
							}
							NOT = { vassal_contract_has_flag = vassal_contract_marriage_favor}
						}
					}
				}
				add = {
					scope:actor = {
						every_vassal = {
							limit = {
								vassal_contract_has_flag = vassal_contract_marriage_favor
								NOT = { is_allied_to = scope:actor }
								NOR = {
									this = scope:recipient
									this = scope:secondary_recipient
								}
							}
							add = medium_prestige_value
						}
					}
					desc = VASSAL_WITH_MARRIAGE_FAVOR
				}
			}
		}

		influence = {
			value = 0
			if = {
				limit = { scope:influence_send_option = yes }
				add = scope:actor.medium_influence_value
				desc = INFLUENCE_INTERACTION_ACCEPTANCE_SEND_OPTION
			}
		}
	}

	interface_priority = 60

	# actor, recipient ... characters arranging the marriage
	# secondary_actor, secondary_recipient ... characters that should marry

	is_shown = {
		# Enable only for yourself if you're a landless adventurer
		trigger_if = {
			limit = {
				scope:actor = {
					is_landless_adventurer = yes
				}
			}
			scope:secondary_actor = {
				scope:actor = this
			}
		}
		scope:secondary_actor = {
			#Is this someone I can marry off?
			OR = {
				is_courtier_of = scope:actor
				AND = {
					is_child_of = scope:actor
					target_is_liege_or_above = scope:actor
				}
				scope:actor = this
			}
			#They can be married
			can_marry_trigger = yes
			#To account for a person marrying their concubines
			trigger_if = {
				limit = {
					is_concubine = yes
				}
				is_concubine_of = scope:actor
				can_marry_character_trigger = { CHARACTER = scope:actor }
			}
		}
		# Block further marriage proposals for people who have a pending grand wedding
		NOR = {
			scope:secondary_actor ?= { has_been_promised_grand_wedding = yes }
			scope:secondary_recipient ?= { has_been_promised_grand_wedding = yes }
		}
	}

	is_valid_showing_failures_only = {
		#Diplomatic availability & prison
		scope:secondary_actor = {
			is_busy_in_events_localised = yes
			is_imprisoned = no
			bp2_valid_for_standard_interactions_trigger = yes
		}
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = secondary_actor }
		trigger_if = {
			limit = {
				NOT = { scope:secondary_actor = scope:secondary_recipient }
			}
			marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = secondary_recipient }
		}
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = actor }
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = recipient }
	}

	has_valid_target_showing_failures_only = {
		marriage_interaction_valid_target_trigger = yes  #Checks marriage status, betrothed, gender, consanguinity, faith hostility etc.

		#Diplomatic availability & prison (located here because these will never be set at the start)
		scope:recipient = {
			is_busy_in_events_localised = yes
			is_imprisoned = no
		}
		NOT = {
			scope:actor = {
				is_at_war_with = scope:recipient
			}
		}
		trigger_if = {
			limit = { NOT = { scope:recipient = scope:secondary_recipient } }
			scope:secondary_recipient = {
				is_busy_in_events_localised = yes
				is_imprisoned = no
				bp2_valid_for_standard_interactions_trigger = yes
			}
		}
		#secondary_actor is checked above because they are always available

		#Blocks AI from marrying player guests
		scope:secondary_recipient = {
			trigger_if = {
				limit = {
					is_pool_guest = yes
					host = {
						is_ai = no
						NOT = { this = scope:actor }
					}
				}
				NOT = { is_pool_guest_of = host }
			}
		}
	}

	on_send = {
		if = {
			limit = {
				scope:grand_wedding_promise = yes
			}
			scope:actor = { 
				set_variable = {
					name = grand_wedding_promise_pending
					days = 10
				}
				if = {
					limit = {
						is_ai = yes
					}
					remove_short_term_gold = ai_wedding_promise_gold_actor
				}
				#if = { # Activity count debugging, left in for QA purposes
				#	limit = {
				#		NOT = {
				#			exists = global_var:wedding_promise
				#		}
				#	}
				#	set_global_variable = {
				#		name = wedding_promise
				#		value = 1
				#	}
				#}
				#else = {
				#	change_global_variable = {
				#		name = wedding_promise
				#		add = 1
				#	}
				#}
			}
		}
	}

	on_accept = {
		marriage_interaction_on_accept_effect = yes

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_gain
			DESC = clan_unity_marry_off.desc
			REVERSE_NON_HOUSE_TARGET = no
		}		

		scope:actor = {
			if = {
				limit = {
					scope:herd_send_option = yes
				}
				pay_herd = {
					target = scope:recipient
					value = scope:actor.domicile.medium_herd_value
				}
			}
			if = {
				limit = {
					has_variable = grand_wedding_promise_pending
				}
				remove_variable = grand_wedding_promise_pending
			}
		}
	}

	on_decline = {
		scope:actor = {
			if = {	
				limit = { NOT = { this = scope:recipient } }
				trigger_event = marriage_interaction.0011	
			}
			if = {
				limit = {
					has_variable = grand_wedding_promise_pending
				}
				remove_variable = grand_wedding_promise_pending
			}
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_loss
			DESC = clan_unity_marry_off_decline.desc
			REVERSE_NON_HOUSE_TARGET = no
		}		
	}

	auto_accept = {
		marriage_interaction_auto_accept_trigger = yes #Strong hook conditions
	}
	
	ai_accept = {
		base = 0 # Should be 0 for all interactions
		
		marriage_ai_accept_modifier = yes

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}
	}

	send_option = {
		flag = grand_wedding_promise
		localization = GRAND_WEDDING_PROMISE

		is_shown = {
			scope:actor = { #Can organize a Grand Wedding
				highest_held_title_tier > tier_barony
				is_landed_or_landless_administrative = yes
				trigger_if = {
					limit = {
						is_ai = yes
					}
					is_at_war = no
				}
				# DLC locked
				has_dlc_feature = tours_and_tournaments
				has_dlc_feature = advanced_activities
			}
		}

		current_description = {
			desc = "GRAND_WEDDING_PROMISE_INFO"
		}

		is_valid = {
			scope:actor = {
				gold >= real_wedding_cost_actor_scope
				is_available = yes
				custom_tooltip = {
					text = grand_wedding_already_promised_tt
					NOR = {
						AND = {
							has_variable = promised_grand_wedding_marriage_countdown
							var:promised_grand_wedding_marriage_countdown = { is_alive = yes }
						}
						has_variable = grand_wedding_promise_pending
					}
				}
			}
			custom_tooltip = {
				text = grand_wedding_only_dominant_side
				trigger_if = {
					limit = {
						scope:secondary_actor ?= {
							is_female = yes
						}
					}
					OR = {
						scope:secondary_actor ?= { matrilinear_betrothal = yes }
						scope:matrilineal = yes
					}
				}
				trigger_else = {
					NOR = {
						scope:secondary_actor ?= { matrilinear_betrothal = yes }
						scope:matrilineal = yes
					}
				}
			}
			custom_tooltip = {
				text = grand_wedding_only_family
				scope:secondary_actor ?= {
					is_close_or_extended_family_of = scope:actor
				}
			}
		}
	}
	
	send_option = {
		flag = matrilineal
		localization = "MATRILINEAL_OFFER"
		starts_enabled = {
			scope:actor = {
				OR = {
					faith = {
						has_doctrine = doctrine_gender_female_dominated
					}
					AND = {
						scope:secondary_actor = { is_female = yes }
						faith = { has_doctrine = doctrine_gender_equal }
					}
					AND = {
						exists = scope:secondary_actor
						scope:actor = scope:secondary_actor
						is_female = yes
						is_ai = no
					}
				}
			}
		}
	}
	
	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = MARRIAGE_HOOK
	}
	#This option cannot be linked with the hook icon, because recipient isn't known in the right-click menu

	send_option = { # EP3 Influence
		is_shown = { # Actor must have a government that uses influence 
			scope:actor = {
				government_has_flag = government_has_influence
			}
		}
		is_valid = {
			# Actor has enough influence
			scope:actor = { influence >= medium_influence_value }
			# Both characters are within the same top realm
			custom_tooltip = {
				text = not_same_realm_tt
				scope:recipient.top_liege = scope:actor.top_liege
			}
		}
		flag = influence_send_option
		localization = TRADE_INFLUENCE_FOR_BETTER_AI_ACCEPTANCE
	}
	send_option = { # MPO Herd
		is_shown = { # Actor must have a government that uses Herd 
			scope:actor = {
				government_has_flag = government_is_nomadic
				is_ai = no
			}
			# Recipient must care about Herd
			scope:recipient = {
				government_has_flag = government_is_nomadic
			}
		}
		is_valid = {
			# Actor has enough Herd
			scope:actor = { domicile.herd >= domicile.medium_herd_value }
		}
		flag = herd_send_option
		localization = TRADE_HERD_FOR_BETTER_AI_ACCEPTANCE
	}
	
	send_options_exclusive = no
	
	show_effects_in_notification = no
	greeting = positive
	
	notification_text = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:secondary_actor = {
						is_adult = yes
					}
					scope:secondary_recipient = {
						is_adult = yes
					}
					NOR = {
						scope:secondary_actor = { has_been_promised_grand_wedding = yes }
						scope:secondary_recipient = { has_been_promised_grand_wedding = yes }
					}
				}
				desc = {
					first_valid = {	
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_actor = scope:actor
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_BOTH_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_actor = scope:actor
							}
							desc = MARRIAGE_NOTIFICATION_ACTOR_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_RECIPIENT_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
							}
							desc = MARRIAGE_NOTIFICATION_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:secondary_actor = scope:actor
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_BOTH_PERSONAL
						}
						triggered_desc = {
							trigger = {
								scope:secondary_actor = scope:actor
							}
							desc = MARRIAGE_NOTIFICATION_ACTOR_PERSONAL
						}
						triggered_desc = {
							trigger = {
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_RECIPIENT_PERSONAL
						}
						desc = MARRIAGE_NOTIFICATION
					}
				}
			}
			desc = {
				first_valid = {	
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_actor = scope:actor
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_BOTH_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_actor = scope:actor
						}
						desc = BETROTHAL_NOTIFICATION_ACTOR_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_RECIPIENT_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
						}
						desc = BETROTHAL_NOTIFICATION_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:secondary_actor = scope:actor
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_BOTH_PERSONAL
					}
					triggered_desc = {
						trigger = {
							scope:secondary_actor = scope:actor
						}
						desc = BETROTHAL_NOTIFICATION_ACTOR_PERSONAL
					}
					triggered_desc = {
						trigger = {
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_RECIPIENT_PERSONAL
					}
					desc = BETROTHAL_NOTIFICATION
				}
			}
		}
	}
}

break_betrothal_interaction = {
	category = interaction_category_diplomacy
	special_interaction = break_betrothal_interaction
	use_diplomatic_range = no
	icon = icon_marriage
	interface_priority = 30
	
	is_shown = {
		OR = {
			#Betrothed to betrothed
			scope:actor.betrothed = scope:recipient

			#Actor breaking recipient's betrothal with their courtier/vassal child
			scope:actor = {
				any_courtier = {
					is_ai = yes
					is_betrothed = yes
					betrothed = { this = scope:recipient }
					OR = {
						is_playable_character = no
						any_parent = { this = scope:actor }
					}
				}
			}

			#Actor breaking the betrothal of their courtier/vassal child
			scope:recipient = {
				is_ai = yes
				is_betrothed = yes
			 	OR = {
			 		AND = {
			 			is_playable_character = no
			 			liege = scope:actor
			 		}
			 		AND = {
			 			is_child_of = scope:actor
			 			NOT = {
			 				matchmaker = {
			 					is_parent_of = prev
			 					NOT = { this = scope:actor }
			 				}
			 			}
			 			OR = {
			 				is_playable_character = no
			 				target_is_liege_or_above = scope:actor
			 			}
			 		}
			 	}
			 	betrothed = { always = yes }
			}

			#Break recipient's betrothal with actor's vassal child/courtier
			scope:recipient = {
				is_betrothed = yes
				betrothed = {
					is_ai = yes
					OR = {
						AND = {
							is_child_of = scope:actor
							target_is_liege_or_above = scope:actor
				 			NOT = {
				 				matchmaker = {
				 					is_parent_of = prev
				 					NOT = { this = scope:actor }
				 				}
				 			}
						}
						AND = {
							is_playable_character = no
							liege = scope:actor
						}
					}
				}
			}

			#Actor breaking their betrothal to recipient's courtier/vassal child
			AND = {
				scope:actor = { is_betrothed = yes }
				scope:recipient = {
					OR = {
						#Check for courtiers
						any_courtier = {
							is_betrothed = yes
							betrothed = scope:actor
							liege = scope:recipient
						}
						#Check for vassal children
						any_child = {
							is_betrothed = yes
							betrothed = scope:actor
							OR = {
								is_playable_character = no
								target_is_liege_or_above = scope:recipient
							}
				 			NOT = {
				 				matchmaker = {
				 					is_parent_of = prev
				 					NOT = { this = scope:actor }
				 				}
				 			}
						}
					}
				}
			}
		}
	}

	is_valid = {
		
	}

	is_valid_showing_failures_only = {
		scope:recipient = { is_busy_in_events_localised = yes }
		scope:recipient = { NOT = { has_strong_hook = scope:actor } }
		# Recipient is the betrothed
		custom_description = {
			text = break_betrothal_hook_used
			object = scope:recipient
			trigger_if = {
				limit = {
					exists = scope:recipient.betrothed
					OR = {
						scope:recipient.betrothed = scope:actor
						scope:recipient.betrothed = {
							is_ai = yes
							OR = {
								AND = {
									is_child_of = scope:actor
									target_is_liege_or_above = scope:actor
						 			NOT = {
						 				matchmaker = {
						 					is_parent_of = prev
						 					NOT = { this = scope:actor }
						 				}
						 			}
								}
								AND = {
									is_playable_character = no
									liege = scope:actor
								}
							}
						}
					}
					exists = scope:recipient.var:hook_used_for_betrothal
				}
				scope:recipient.var:hook_used_for_betrothal = {
					NOT = { this = scope:recipient.betrothed }
				}
			}
		}

		custom_description = {
			text = break_betrothal_hook_used
			object = scope:recipient
			trigger_if = {
				limit = {
					exists = scope:recipient.betrothed
					OR = {
						scope:recipient = scope:actor
						scope:recipient = {
							is_ai = yes
							OR = {
								AND = {
									is_child_of = scope:actor
									target_is_liege_or_above = scope:actor
						 			NOT = {
						 				matchmaker = {
						 					is_parent_of = prev
						 					NOT = { this = scope:actor }
						 				}
						 			}
								}
								AND = {
									is_playable_character = no
									liege = scope:actor
								}
							}
						}
					}
					exists = scope:recipient.betrothed.var:hook_used_for_betrothal
				}
				scope:recipient.betrothed.var:hook_used_for_betrothal = {
					NOT = { this = scope:recipient }
				}
			}
		}

		# Secondary Recipient is the betrothed
		custom_description = {
			text = break_betrothal_hook_used
			object = scope:recipient
			trigger_if = {
				limit = {
					scope:recipient = {
						OR = {
							#Check for courtiers
							any_courtier = {
								is_betrothed = yes
								betrothed = scope:actor
								liege = scope:recipient
								exists = var:hook_used_for_betrothal
								var:hook_used_for_betrothal = {
									this = scope:actor
								}
							}
							#Check for vassal children
							any_child = {
								is_betrothed = yes
								betrothed = scope:actor
								OR = {
									is_playable_character = no
									target_is_liege_or_above = scope:recipient
								}
					 			NOT = {
					 				matchmaker = {
					 					is_parent_of = prev
					 					NOT = { this = scope:actor }
					 				}
					 			}
								exists = var:hook_used_for_betrothal
								var:hook_used_for_betrothal = {
									this = scope:actor
								}
							}
						}
					}
				}
				always = no
			}
		}

		# Secondary Actor and Recipient were promised a Grand Wedding, and the Activity has already been organized and is ongoing
		custom_description = {
			text = break_betrothal_gwedding_ongoing
			trigger_if = {
				limit = {
					OR = {
						scope:recipient = { has_been_promised_grand_wedding = yes }
						scope:recipient.betrothed = { has_been_promised_grand_wedding = yes }
					}
				}
				NOR = {
					scope:recipient.involved_activity ?= {
						has_activity_type = activity_wedding
						OR = {
							special_guest:spouse_1 ?= scope:recipient
							special_guest:spouse_2 ?= scope:recipient
							special_guest:spouse_1 ?= scope:recipient.betrothed
							special_guest:spouse_2 ?= scope:recipient.betrothed
						}
					}
					scope:recipient.betrothed.involved_activity ?= {
						has_activity_type = activity_wedding
						OR = {
							special_guest:spouse_1 ?= scope:recipient
							special_guest:spouse_2 ?= scope:recipient
							special_guest:spouse_1 ?= scope:recipient.betrothed
							special_guest:spouse_2 ?= scope:recipient.betrothed
						}
					}
				}
			}
		}
	}

	on_accept = {
		#Identifies the "injured party"
		scope:recipient = {
			if = {
				limit = { matchmaker = scope:actor }
				betrothed = { save_scope_as = rejected_betrothed }
				save_scope_as = rejecting_betrothed
			}
			else_if = {
				limit = {
					OR = {
						#Check for courtiers
						any_courtier = {
							is_betrothed = yes
							is_ai = yes
							betrothed = scope:actor
							liege = scope:recipient
						}
						#Check for vassal children
						any_child = {
							is_betrothed = yes
							is_ai = yes
							betrothed = scope:actor
							target_is_liege_or_above = scope:recipient
						}
					}
				}
				scope:actor.betrothed = {
					save_scope_as = rejected_betrothed
				}
				scope:actor = {
					save_scope_as = rejecting_betrothed
				}
			}

			else = {
				save_scope_as = rejected_betrothed
				betrothed = { save_scope_as = rejecting_betrothed }
			}
		}

		# If it was a Grand Wedding, find the host
		scope:rejecting_betrothed.var:promised_grand_wedding_by ?= {
			save_scope_as = promising_host
		}

		scope:recipient = { trigger_event = marriage_interaction.0020 }

		#Tooltip for betrothal breakage
		show_as_tooltip = {
			scope:rejected_betrothed = {
				break_betrothal = scope:rejecting_betrothed
			}
		}

		#Figures out if the liege of rejected_betrothed cares
		scope:rejected_betrothed = {
			if = {
				limit = { #The liege matter when assigning prestige/opinion hits
					matchmaker = {
						NOT = { this = scope:rejected_betrothed }
						is_close_or_extended_family_of = scope:rejected_betrothed
					}
				}

				matchmaker = { save_scope_as = rejected_betrothal_owner }
			}

			else = { #The liege doesn't matter, only the betrothed
				save_scope_as = rejected_betrothal_owner
			}
		}

		if = {
			limit = { #Only if you had no reason to break the betrothal
				scope:rejected_betrothed = { is_eunuch_trigger = no }
				scope:rejecting_betrothed = { is_eunuch_trigger = no }
				NOR = {
					scope:rejected_betrothed = { has_trait_with_flag = can_not_marry }
					scope:rejecting_betrothed = { has_trait_with_flag = can_not_marry }
					scope:rejected_betrothed = {
						allowed_to_marry_same_sex_trigger = no
						sex_same_as = scope:rejecting_betrothed
					}
					scope:rejecting_betrothed = {
						allowed_to_marry_same_sex_trigger = no
						sex_same_as = scope:rejected_betrothed
					}
					# Prestige loss for Grand Weddings is handled below in the specific effect
					scope:rejecting_betrothed = { has_been_promised_grand_wedding = yes }
					scope:rejected_betrothed = { has_been_promised_grand_wedding = yes }
				}
			}
			#Prestige penalty
			if = {
				limit = {
					OR = {
						scope:rejected_betrothed = { highest_held_title_tier = tier_empire }
						scope:rejected_betrothal_owner = { highest_held_title_tier = tier_empire }
					}
					#Only applied for relevant characters
					OR = {
						scope:rejected_betrothed = {
							OR = {
								is_close_family_of = scope:actor
								AND = {
									exists = dynasty
									exists = scope:actor.dynasty
									dynasty = scope:actor.dynasty
								}
							}
						}
						scope:rejecting_betrothed = {
							OR = {
								is_close_family_of = scope:actor
								AND = {
									exists = dynasty
									exists = scope:actor.dynasty
									dynasty = scope:actor.dynasty
								}
							}
						}
						scope:actor = {
							yields_alliance = {
								candidate = scope:rejecting_betrothed
								target = scope:rejected_betrothal_owner
								target_candidate = scope:rejected_betrothed
							}
						}
					}
				}
				scope:actor = { add_prestige = massive_prestige_loss }
			}

			else_if = {
				limit = {
					OR = {
						scope:rejected_betrothed = { highest_held_title_tier = tier_kingdom }
						scope:rejected_betrothal_owner = { highest_held_title_tier = tier_kingdom }
					}
					#Only applied for relevant characters
					OR = {
						scope:rejected_betrothed = {
							OR = {
								is_close_family_of = scope:actor
								AND = {
									exists = dynasty
									exists = scope:actor.dynasty
									dynasty = scope:actor.dynasty
								}
							}
						}
						scope:rejecting_betrothed = {
							OR = {
								is_close_family_of = scope:actor
								AND = {
									exists = dynasty
									exists = scope:actor.dynasty
									dynasty = scope:actor.dynasty
								}
							}
						}
						scope:actor = {
							yields_alliance = {
								candidate = scope:rejecting_betrothed
								target = scope:rejected_betrothal_owner
								target_candidate = scope:rejected_betrothed
							}
						}
					}
				}
				scope:actor = { add_prestige = major_prestige_loss }
			}

			else_if = {
				limit = {
					OR = {
						scope:rejected_betrothed = { highest_held_title_tier = tier_duchy }
						scope:rejected_betrothal_owner = { highest_held_title_tier = tier_duchy }
					}
					#Only applied for relevant characters
					OR = {
						scope:rejected_betrothed = {
							OR = {
								is_close_family_of = scope:actor
								AND = {
									exists = dynasty
									exists = scope:actor.dynasty
									dynasty = scope:actor.dynasty
								}
							}
						}
						scope:rejecting_betrothed = {
							OR = {
								is_close_family_of = scope:actor
								AND = {
									exists = dynasty
									exists = scope:actor.dynasty
									dynasty = scope:actor.dynasty
								}
							}
						}
						scope:actor = {
							yields_alliance = {
								candidate = scope:rejecting_betrothed
								target = scope:rejected_betrothal_owner
								target_candidate = scope:rejected_betrothed
							}
						}
					}
				}
				scope:actor = { add_prestige = medium_prestige_loss }
			}

			else_if = {
				limit = {
					OR = {
						scope:rejected_betrothed = { highest_held_title_tier = tier_county }
						scope:rejected_betrothal_owner = { highest_held_title_tier = tier_county }
					}
					#Only applied for relevant characters
					OR = {
						scope:rejected_betrothed = {
							OR = {
								is_close_family_of = scope:actor
								AND = {
									exists = dynasty
									exists = scope:actor.dynasty
									dynasty = scope:actor.dynasty
								}
							}
						}
						scope:rejecting_betrothed = {
							OR = {
								is_close_family_of = scope:actor
								AND = {
									exists = dynasty
									exists = scope:actor.dynasty
									dynasty = scope:actor.dynasty
								}
							}
						}
						scope:actor = {
							yields_alliance = {
								candidate = scope:rejecting_betrothed
								target = scope:rejected_betrothal_owner
								target_candidate = scope:rejected_betrothed
							}
						}
					}
				}
				scope:actor = { add_prestige = minor_prestige_loss }
			}

			else_if = {
				limit = {
					OR = {
						scope:rejected_betrothed = { highest_held_title_tier = tier_barony }
						scope:rejected_betrothal_owner = { highest_held_title_tier = tier_barony }
					}
					#Only applied for relevant characters
					OR = {
						scope:rejected_betrothed = {
							OR = {
								is_close_family_of = scope:actor
								AND = {
									exists = dynasty
									exists = scope:actor.dynasty
									dynasty = scope:actor.dynasty
								}
							}
						}
						scope:rejecting_betrothed = {
							OR = {
								is_close_family_of = scope:actor
								AND = {
									exists = dynasty
									exists = scope:actor.dynasty
									dynasty = scope:actor.dynasty
								}
							}
						}
						scope:actor = {
							yields_alliance = {
								candidate = scope:rejecting_betrothed
								target = scope:rejected_betrothal_owner
								target_candidate = scope:rejected_betrothed
							}
						}
					}
				}
				scope:actor = { add_prestige = miniscule_prestige_loss }
			}
		}

		############################################
		# EP2 - Breaking a Grand Wedding Betrothal #
		############################################
		if = {
			limit = {
				# There is no forced reason for breaking the betrothal
				scope:rejected_betrothed = { is_eunuch_trigger = no }
				scope:rejecting_betrothed = { is_eunuch_trigger = no }
				NOR = {
					scope:rejected_betrothed = { has_trait_with_flag = can_not_marry }
					scope:rejecting_betrothed = { has_trait_with_flag = can_not_marry }
					scope:rejecting_betrothed = {
						allowed_to_marry_same_sex_trigger = no
						sex_same_as = scope:rejected_betrothed
					}
					scope:rejected_betrothed = {
						allowed_to_marry_same_sex_trigger = no
						sex_same_as = scope:rejecting_betrothed
					}
				}
				# A Grand Wedding was promised
				OR = {
					scope:rejecting_betrothed = {
						has_been_promised_grand_wedding = yes
					}
					scope:rejected_betrothed = {
						has_been_promised_grand_wedding = yes
					}
				}
			}
			scope:actor = { break_grand_wedding_betrothal_effect = yes }
			scope:rejected_betrothed = { trigger_event = marriage_interaction.0001 }
		}

		######################################
		# Opinion hits & notification events #
		######################################
		#Personal opinion hit for the rejected_betrothed
		else_if = {
			limit = { NOT = { scope:rejected_betrothed = scope:actor } }
			scope:rejected_betrothed = {
				if = {
					limit = {
						is_eunuch_trigger = no
						scope:rejecting_betrothed = { is_eunuch_trigger = no }
						NOR = {
							has_trait_with_flag = can_not_marry
							scope:rejecting_betrothed = { has_trait_with_flag = can_not_marry }
							scope:rejecting_betrothed = {
								allowed_to_marry_same_sex_trigger = no
								sex_same_as = scope:rejected_betrothed
							}
							AND = {
								allowed_to_marry_same_sex_trigger = no
								sex_same_as = scope:rejecting_betrothed
							}
						}
					}
					show_as_tooltip = {
						add_opinion = {
							target = scope:actor
							modifier = broke_betrothal_opinion
						}
					}
				}

				hidden_effect = { trigger_event = marriage_interaction.0001 }
			}
		}
		else = {
			hidden_effect = {
				scope:rejecting_betrothed = {
					break_betrothal = scope:rejected_betrothed
				}
			}
		}

		#Personal opinion hit for the rejected_betrothal_owner
		if = {
			limit = {
				NOR = {
					scope:rejected_betrothed = {
						this = scope:rejected_betrothal_owner
					}
					scope:actor = {
						this = scope:rejected_betrothal_owner
					}
					scope:actor = {
						this = scope:rejected_betrothed
					}
				}
			}
			scope:rejected_betrothal_owner = {
				if = {
					limit = {
						scope:rejected_betrothed = { is_eunuch_trigger = no }
						scope:rejecting_betrothed = { is_eunuch_trigger = no }
						NOR = {
							scope:rejected_betrothed = { has_trait_with_flag = can_not_marry }
							scope:rejecting_betrothed = { has_trait_with_flag = can_not_marry }
							scope:rejected_betrothed = {
								allowed_to_marry_same_sex_trigger = no
								sex_same_as = scope:rejecting_betrothed
							}
							scope:rejecting_betrothed = {
								allowed_to_marry_same_sex_trigger = no
								sex_same_as = scope:rejected_betrothed
							}
						}
					}
					if = {
						limit = {
							NOR = {
								scope:rejected_betrothed = { has_been_promised_grand_wedding = yes }
								scope:rejecting_betrothed = { has_been_promised_grand_wedding = yes }
							}
						}
						show_as_tooltip = {
							add_opinion = {
								target = scope:actor
								modifier = broke_betrothal_opinion
							}
						}
					}
				}

				hidden_effect = { trigger_event = marriage_interaction.0001 }
			}
		}

		if = {
			limit = {
				scope:rejected_betrothed = { is_eunuch_trigger = no }
				scope:rejecting_betrothed = { is_eunuch_trigger = no }
				NOR = {
					scope:rejected_betrothed = { has_trait_with_flag = can_not_marry }
					scope:rejecting_betrothed = { has_trait_with_flag = can_not_marry }
					scope:rejected_betrothed = {
						allowed_to_marry_same_sex_trigger = no
						sex_same_as = scope:rejecting_betrothed
					}
					scope:rejecting_betrothed = {
						allowed_to_marry_same_sex_trigger = no
						sex_same_as = scope:rejected_betrothed
					}
				}
			}
			#Opinion hit for close relatives of the rejected_betrothed (that are not rejected_betrothed_owner)
			if = {
				limit = {
					NOT = { scope:rejected_betrothed = scope:actor }
					scope:rejected_betrothed = {
						any_close_or_extended_family_member = {
							NOT = { this = scope:rejected_betrothal_owner }
							count >= 1
						}
					}
				}
				if = {
					limit = {
						OR = {
							scope:rejected_betrothed = { has_been_promised_grand_wedding = yes }
							scope:rejecting_betrothed = { has_been_promised_grand_wedding = yes }
						}
					}
					scope:rejected_betrothed = {
						every_close_or_extended_family_member = {
							limit = {
								NOT = { this = scope:rejected_betrothal_owner }
							}
							custom = all_family_members
							
							add_opinion = {
								target = scope:actor
								modifier = broke_betrothal_grand_wedding_opinion
								opinion = -50
							}
						}
					}
				}
				else = {
					scope:rejected_betrothed = {
						every_close_or_extended_family_member = {
							limit = {
								NOT = { this = scope:rejected_betrothal_owner }
							}
							custom = all_family_members
							add_opinion = {
								target = scope:actor
								modifier = broke_betrothal_opinion
							}
						}
					}
				}
			}

			#Opinion hit for close relatives of the rejecting_betrothed (that are not also close relatives to rejected_betrothed)
			if = {
				limit = {
					NOR = {
						scope:rejecting_betrothed = scope:actor
						scope:rejected_betrothed = scope:actor
					}
					scope:rejecting_betrothed = {
						any_close_or_extended_family_member = {
							NOR = {
								is_close_or_extended_family_of = scope:rejected_betrothed
								this = scope:rejected_betrothal_owner
								this = scope:rejected_betrothed
							}
							count >= 1
						}
					}
				}
				if = {
					limit = {
						OR = {
							scope:rejected_betrothed = { has_been_promised_grand_wedding = yes }
							scope:rejecting_betrothed = { has_been_promised_grand_wedding = yes }
						}
					}
					scope:rejecting_betrothed = {
						every_close_or_extended_family_member = {
							limit = {
								NOR = {
									this = scope:rejected_betrothal_owner
									is_close_or_extended_family_of = scope:rejected_betrothed
									this = scope:rejected_betrothed
								}
							}
							custom = all_family_members
							
							add_opinion = {
								target = scope:actor
								modifier = broke_betrothal_grand_wedding_opinion
								opinion = -50
							}
						}
					}
				}
				else = {
					scope:rejecting_betrothed = {
						every_close_or_extended_family_member = {
							limit = {
								NOR = {
									is_close_or_extended_family_of = scope:rejected_betrothed
									this = scope:rejected_betrothal_owner
									this = scope:rejected_betrothed
								}
							}
							custom = all_family_members
							add_opinion = {
								target = scope:actor
								modifier = broke_betrothal_opinion
							}
						}
					}
				}
			}
		}

		scope:actor = {
		# Was the marriage of relevence to the player?
			if = {
				limit = { 
					scope:rejecting_betrothed = {
						is_player_heir_of = scope:actor
					}
				}
				hidden_effect = {
					send_interface_toast = {
						type = msg_break_betrothal_player_heir
						title = msg_break_betrothal
						left_icon = scope:rejecting_betrothed
						right_icon = scope:rejected_betrothed
						custom_tooltip = break_betrothal_hook_toast_desc
					}
				}
			}
			else_if = {
				limit = {
					NOT = {
						scope:actor = scope:rejecting_betrothed
					}
				}
				hidden_effect = {
					send_interface_message = {
						type = msg_break_betrothal
						title = msg_break_betrothal
						left_icon = scope:rejecting_betrothed
						right_icon = scope:rejected_betrothed
						custom_tooltip = break_betrothal_hook_toast_desc
					}
				}
			}

			#Was the marriage promised to a vassal?
			if = {
				limit = {
					exists = var:promised_to_marry_1
					var:promised_to_marry_1 = {
						this = scope:rejected_betrothed
						any_parent = {
							is_vassal_of = scope:actor
						}
					}
					exists = var:promised_to_marry_2
					var:promised_to_marry_2 = {
						this = scope:rejecting_betrothed
						is_close_family_of = scope:actor
					}
				}
				remove_variable = promised_to_marry_1
				remove_variable = promised_to_marry_2
				trigger_event = clan.1002
			}
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = major_unity_loss
			DESC = clan_unity_betrothal_breakage.desc
			REVERSE_NON_HOUSE_TARGET = no
		}		
	}

	auto_accept = yes
}

# Stripped down version of make_concubine_interaction for the AI
# Avoids redirection logic and recipient list building to improve performance
ai_make_concubine_interaction = {
	hidden = yes
	
	ai_targets = {
		ai_recipients = prisoners
	}
	ai_targets = {
		ai_recipients = courtiers
	}
	ai_target_quick_trigger = {
		adult = yes
		owner_attracted = yes # Not optimal, but saves a lot of performance
	}
	ai_frequency = 12

	ai_potential = {
		is_playable_character = yes
		is_adult = yes
		fertility > 0
		allowed_more_concubines = yes
		NOR = {
			has_trait = chaste
			has_trait = celibate
			has_sexuality = asexual
		}
	}

	is_shown = {
		make_concubine_is_shown_trigger = yes
	}

	is_valid_showing_failures_only = {
		make_concubine_is_valid_trigger = yes
	}

	auto_accept = yes

	ai_will_do = {
		base = 0
		
		modifier = { # Basic Filtering
			add = 100
			scope:actor = {
				number_of_concubines < number_of_desired_concubines
			}
			scope:recipient = {
				can_have_children_with = { CHARACTER = scope:actor } # Only if you can have kids since this is about fertility and genetic traits
				fertility > 0.2
				is_young_character = yes
			}
		}
		
		modifier = {
			add = 100
			scope:recipient = {
				is_imprisoned_by = scope:actor
				can_have_children_with = { CHARACTER = scope:actor } # Only if you can have kids since this is about fertility and genetic traits
				OR = {
					is_lowborn = no
					num_of_good_genetic_traits > 0
				}
				fertility > 0.1
				trigger_if = {
					limit = {
						is_female = yes
					}
					is_aging_character = no
				}
			}
		}

		modifier = {
			add = 100
			scope:actor = {
				allowed_to_marry_same_sex_trigger = yes
				sex_same_as = scope:recipient
				is_attracted_to_gender_of = scope:recipient
				OR = { #If they're not in need of kids, or someone who they can have kids with
					any_child = {
						count >= 2
					}
					any_consort = {
						can_have_children_with = { CHARACTER = scope:actor }
						fertility > 0.1
					}
				}
			}
		}

		modifier = { #Even if you're allowed to take same-sex concubines we don't want the AI to do so without a good reason if they're not attracted to the person
			add = -200
			scope:actor = {
				allowed_to_marry_same_sex_trigger = yes
				sex_same_as = scope:recipient
				NOT = { is_attracted_to_gender_of = scope:recipient }
			}
		}

		modifier = { # If they don't have a batch of kids and opposite sex consorts to handle that situation
			add = -200
			scope:actor = {
				allowed_to_marry_same_sex_trigger = yes
				sex_same_as = scope:recipient
				NOT = {
					any_child = {
						count >= 2
					}
				}
				NOT = {
					any_consort = {
						can_have_children_with = { CHARACTER = scope:actor }
						fertility > 0.1
					}
				}
			}
		}

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}

		modifier = { # Incest Filtering: Hard NO!
			factor = 0
			scope:actor = {
				relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:recipient }
				NOR = {
					accepts_incest_with_trigger = { CHARACTER = scope:recipient }
					sexually_liberal_trigger = yes
					AND = {
						AND = {
							exists = primary_heir
							primary_heir = { NOT = { dynasty = root.dynasty }	}
						}
						NOT = {
							primary_title = {
								any_title_heir = {	dynasty = root.dynasty 	}
							}
						}
					}
				}
			}
		}

		modifier = { # Incest Filtering: Not likely, but keep the door open...
			factor = 0.2 
			scope:actor = {
				relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:recipient }
				OR = {
					accepts_incest_with_trigger = { CHARACTER = scope:recipient }
					sexually_liberal_trigger = yes
					AND = {
						exists = primary_heir
						primary_heir = { NOT = { dynasty = root.dynasty }	}
						NOT = {
							primary_title = {
								any_title_heir = {	dynasty = root.dynasty 	}
							}
						}
					}
				}
			}
		}

		modifier = { # Less likely if married to a player and the target is the player heir 
			factor = 0.4
			scope:actor = {
				any_spouse = {
					is_ai = no
					scope:recipient = {	
						OR = {
							is_primary_heir_of = prev 
							is_heir_of = prev
						}
					}
				}
			}
		}
	}

	on_accept = {
		scope:actor = {
			save_scope_as = secondary_actor
		}
		scope:recipient = {
			save_scope_as = secondary_recipient
		}
		concubine_on_accept_effect = yes

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_gain
			DESC = clan_unity_concubinage.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
}

make_concubine_interaction = {
	category = interaction_category_diplomacy
	icon = concubine_icon

	desc = make_concubine_interaction_desc

	interface = concubine_list
	

	needs_recipient_to_open = yes

	redirect = {
		# The UI will open to the already filled left and right first if we don't tell it we've got both sides filled
		scope:actor = {
			save_scope_as = secondary_actor
		}
		scope:recipient = {
			save_scope_as = secondary_recipient
		}
	}

	populate_recipient_list = {
		scope:actor = {
			every_courtier = {
				limit = {
					can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
				}
				add_to_list = characters
			}
			every_prisoner = {
				limit = {
					NOT = { is_in_list = characters }
					can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
				}
				add_to_list = characters
			}
		}
	}
	
	is_shown = {
		make_concubine_is_shown_trigger = yes
	}

	is_valid_showing_failures_only = {
		make_concubine_is_valid_trigger = yes
	}
	
	auto_accept = yes
	
	on_accept = {
		concubine_on_accept_effect = yes

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_gain
			DESC = clan_unity_concubinage.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
}

find_concubine = {
	category = interaction_category_diplomacy
	icon = concubine_icon

	desc = find_concubine_desc

	interface = concubine_list

	needs_recipient_to_open = no

	redirect = {
		scope:actor = { # The UI will open to the already filled left first if we don't tell it we've got "our side" actor which is always ourselves
			save_scope_as = secondary_actor
		}
	}

	populate_recipient_list = {
		scope:actor = {
			every_courtier = {
				limit = {
					can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
				}
				add_to_list = characters
			}
			every_prisoner = {
				limit = {
					NOT = { is_in_list = characters }
					can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
				}
				add_to_list = characters
			}
		}
	}

	is_shown = {
		scope:actor = {
			allowed_concubines = yes
			is_adult = yes
			trigger_if = { #So that we show the recipient list, but can also only use this on ourselves, we have to do this check in the beginning
				limit = {
					NOT = { exists = scope:secondary_recipient }
				}
				scope:actor = scope:recipient
			}
		}
		trigger_if = {
			limit = {
				NOT = { scope:actor = scope:recipient }
			}
			scope:actor = {
				trigger_if = {
					limit = {
						allowed_to_marry_same_sex_trigger = no
					}
					sex_opposite_of = scope:recipient
				}
				NOT = { relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:recipient } }
			}
			scope:recipient = {
				can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
				is_adult = yes
				OR = {
					is_imprisoned_by = scope:actor
					is_courtier_of = scope:actor
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		custom_description = {
			text = take_concubine_allowed_more
			object = scope:recipient
			scope:actor = {
				allowed_more_concubines = yes
			}
		}
		custom_description = {
			text = take_concubine_available_character_opp
			object = scope:actor
			scope:actor = {
				OR = {
					any_courtier = {
						can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
					}
					any_prisoner = {
						can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
					}
				}
			}
		}
		trigger_if = {
			limit = {
				NOT = { scope:actor = scope:recipient }
			}
			scope:recipient = {
				can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
			}
		}
	}

	can_be_picked = {
		is_adult = yes
	}

	auto_accept = yes

	ai_will_do = {
		base = 0
	}
	
	on_accept = {
		concubine_on_accept_effect = yes

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_gain
			DESC = clan_unity_concubinage.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
}

offer_concubine = {
	category = interaction_category_diplomacy
	icon = concubine_icon

	desc = offer_concubine_desc

	ai_targets = {
		ai_recipients = neighboring_rulers
		ai_recipients = suzerain
		ai_recipients = liege
		max = 5
	}
	ai_target_quick_trigger = {
		adult = yes
	}
	ai_frequency = 36

	greeting = positive
	notification_text = OFFER_CONCUBINE_NOTIFICATION

	#interface = concubine_list

	needs_recipient_to_open = yes

	populate_actor_list = {
		scope:actor = {
			every_courtier = {
				limit = {
					can_be_offered_as_concubine_to_character_trigger = {
						GIVER = scope:actor
						CHARACTER = scope:recipient
					}
				}
				add_to_list = characters
			}
			every_prisoner = {
				limit = {
					NOT = { is_in_list = characters }
					can_be_offered_as_concubine_to_character_trigger = {
						GIVER = scope:actor
						CHARACTER = scope:recipient
					}
				}
				add_to_list = characters
			}
		}
	}

	is_shown = {
		NOT = { scope:actor = scope:recipient }
		scope:actor = {
			OR = {
				culture = { has_cultural_tradition = tradition_concubines }
				faith = { has_doctrine = doctrine_concubines }
			}		
		}
		scope:recipient = {
			NOT = { government_has_flag = government_is_herder }
			allowed_concubines = yes
			is_adult = yes
		}
	}

	is_valid_showing_failures_only = {
		custom_description = {
			text = take_concubine_available_character_opp
			object = scope:recipient
			scope:actor = {
				OR = {
					any_courtier = {
						can_be_offered_as_concubine_to_character_trigger = {
							GIVER = scope:actor
							CHARACTER = scope:recipient
						}
					}
					any_prisoner = {
						can_be_offered_as_concubine_to_character_trigger = {
							GIVER = scope:actor
							CHARACTER = scope:recipient
						}
					}
				}
			}
		}
		custom_description = {
			text = take_concubine_allowed_more
			object = scope:recipient
			scope:recipient = {
				allowed_more_concubines = yes
			}
		}
	}

	can_be_picked = {
		is_adult = yes
	}

	can_send = {
		custom_description = {
			text = character_unavailable_to_be_offered_as_concubine
			object = scope:secondary_actor
			scope:secondary_actor = {
				can_be_offered_as_concubine_to_character_trigger = {
					GIVER = scope:actor
					CHARACTER = scope:recipient
				}
			}
		}
	}

	auto_accept = no

	ai_accept = {
		base = 50
		
		modifier = {
			add = {
				value = 50
				multiply = scope:secondary_actor.num_of_good_genetic_traits
			}
			scope:secondary_actor = {
				num_of_good_genetic_traits > 0
				trigger_if = {
					limit = {
						is_female = yes
					}
					is_aging_character = no
				}
			}
			desc = AI_POSITIVE_TRAIT_REASON
		}

		modifier = {
			add = {
				value = 50
			}
			scope:secondary_actor = {
				is_lowborn = no
				trigger_if = {
					limit = {
						is_female = yes
					}
					is_aging_character = no
				}
			}
			desc = AI_DYNASTY_PRESTIGE_REASON_NOBLE
		}

		modifier = {
			add = {
				value = -50
				multiply = scope:secondary_actor.num_of_bad_genetic_traits
			}
			scope:secondary_actor = {
				OR = {
					num_of_bad_genetic_traits > 0
				}
				trigger_if = {
					limit = {
						is_female = yes
					}
					is_aging_character = no
				}
			}
			desc = AI_NEGATIVE_TRAIT_REASON
		}

		modifier = {
			add = {
				value = -50
			}
			scope:secondary_actor = {
				is_lowborn = yes
				num_of_good_genetic_traits = 0
				trigger_if = {
					limit = {
						is_female = yes
					}
					is_aging_character = no
				}
			}
			desc = AI_DYNASTY_PRESTIGE_REASON_LOWBORN_NO_POSITIVE_TRAIT
		}

		opinion_modifier = { # Opinion of you
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 1.0
			desc = AI_OPINION_REASON
		}

		opinion_modifier = { # Opinion of concubine
			who = scope:recipient
			opinion_target = scope:secondary_actor
			multiplier = 0.25
			desc = AI_OPINION_REASON
		}

		compare_modifier = { # Recipient is reluctant to marry old women (procreation is a key factor)
			trigger = {
				scope:secondary_actor = {
					is_young_character = no
					is_female = yes
				}
			}
			target = scope:secondary_actor
			value = age
			multiplier = -5.0
			step = 1
			offset = -29
			desc = MARRY_AGE
		}

		modifier = { # A Recipient is unwilling to take a same-sex concubine if their faith doesn't support it
			add = -1000
			
			scope:recipient = {
				allowed_to_marry_same_sex_trigger = no
				sex_same_as = scope:secondary_actor
			}
			desc = SAME_SEX_MARRIAGE_FAITH_REASON_RECIPIENT
		}

		modifier = { # Unwilling if they don't have a batch of kids and opposite sex consorts to handle that situation
			add = -200
			scope:recipient = {
				allowed_to_marry_same_sex_trigger = yes
				sex_same_as = scope:secondary_actor
				trigger_if = {
					limit = {
						NOT = {
							any_consort = {
								can_have_children_with = { CHARACTER = scope:recipient }
								fertility > 0.1
							}
						}
					}
					any_child = { is_player_heir_of = root }
				}
				trigger_else = {
					always = no
				}
			}
			desc = AI_NO_FERTILITY_REASON
		}

		modifier = {
			add = 50
			scope:recipient = {
				is_attracted_to_gender_of = scope:secondary_actor
			}
			desc = AI_ATTRACTION_REASON
		}

		modifier = { # A Recipient is more likely to agree if you're their liege
			add = 40
			
			scope:recipient = {
				target_is_liege_or_above = scope:actor
			}
			desc = LIEGE_REASON
		}

		#More/Less likely based on difference in rank between actor and recipient
		modifier = {
			add = 20
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 1
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = 30
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 2
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = 40
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 3
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = 50
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 4
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = 60
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 5
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = -20
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -1
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = -30
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -2
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = -40
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -3
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = -50
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -4
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = -60
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -5
				}
			}
			desc = AI_RANK_DIFF
		}
		
		modifier = {
			add = intimidated_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		
		modifier = {
			add = intimidated_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		modifier = {
			add = -250
			scope:secondary_actor = {
				has_std_trigger = yes
			}
			desc = AI_STD_REASON
		}
		modifier = {
			add = -250
			scope:secondary_actor = {
				has_epidemic_disease_trigger = yes
			}
			desc = AI_CONTAGIOUS_REASON
		}
		modifier = {
			add = -5000
			scope:secondary_actor = {
				fertility <= 0
			}
			desc = AI_FERTILITY_REASON
		}
		modifier = {
			scope:hook = yes
			add = 500
			scope:actor = { NOT = { has_strong_hook = scope:recipient } }
			desc = SCHEME_WEAK_HOOK_USED
		}
		modifier = {
			scope:hook = yes
			add = 1000
			scope:actor = { has_strong_hook = scope:recipient }
			desc = SCHEME_STRONG_HOOK_USED
		}

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}
	}

	ai_potential = {
		is_adult = yes
		NOT = { has_trait = incapable }
		is_at_war = no
		NOR = {
			has_trait = greedy
			ai_greed > very_high_positive_ai_value
		}	
		OR = {
			culture = { has_cultural_tradition = tradition_concubines }
			faith = { has_doctrine = doctrine_concubines }
		}
	}

	ai_will_do = {
		base = 0
		
		modifier = { # Basic Filtering
			add = 100
			scope:recipient = {
				number_of_concubines < number_of_desired_concubines
			}
			scope:secondary_actor = {
				trigger_if = {
					limit = {
						allowed_to_marry_same_sex_trigger = no
					}
					sex_opposite_of = scope:recipient
				}
				fertility > 0.2
				is_young_character = yes
				NOR = {
					is_councillor_of = scope:actor
					is_of_major_interest_trigger = {
						CHARACTER = scope:actor
					}
					is_concubine_of = scope:actor
				}
			}
		}

		modifier = { # If secondary_actor is close to actor, a good character, or if actor should take them as concubine
			add = -100
			scope:secondary_actor = {
				is_of_minor_interest_trigger = {
					CHARACTER = scope:actor
				}
				AND = {
					can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
					scope:actor = { number_of_concubines < number_of_desired_concubines }
				}
			}
		}

		modifier = { # If secondary_actor could be an agent...
			add = 25
			scope:actor = {
				any_scheme = {
					scheme_target_character = scope:recipient
					save_temporary_scope_as = scheme_check
				}
			}
			scope:secondary_actor = {
				char_can_fit_into_scheme_trigger = { SCHEME = scope:scheme_check }
			}
		}
		
		modifier = { # More likely if it's a neighboring ruler that you want to like you
			add = 50
			scope:recipient = {
				any_neighboring_top_liege_realm_owner = { this = scope:actor }
				max_military_strength > scope:actor.max_military_strength
			}
		}
		
		modifier = { # More likely if it's your friend and it's a nice concubine
			add = 50
			scope:recipient = {
				OR = {
					has_relation_friend = scope:actor
					has_relation_best_friend = scope:actor
				}
			}
			scope:secondary_actor = {
				fertility > 0.2
				is_young_character = yes
				OR = {
					has_trait = lustful
					num_of_good_genetic_traits > 0
				}
			}
		}

		modifier = { #Even if they're allowed to take same-sex concubines we don't want the AI to suggest it without a good reason if they're not attracted to the person
			add = -200
			scope:recipient = {
				allowed_to_marry_same_sex_trigger = yes
				sex_same_as = scope:secondary_actor
				NOT = { is_attracted_to_gender_of = scope:secondary_actor }
			}
		}

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}

		modifier = { # No if it's a same-sex concubinage and actor isn't accepting of it
			factor = 0
			scope:actor = {
				allowed_to_marry_same_sex_trigger = no
				scope:recipient = { sex_same_as = scope:secondary_actor }
			}
		}

		modifier = { # No if it's a same-sex concubinage and recipient isn't accepting of it
			factor = 0
			scope:recipient = {
				allowed_to_marry_same_sex_trigger = no
				sex_same_as = scope:secondary_actor
			}
		}

		modifier = { # Age considerations
			factor = 0
			scope:recipient = {
				OR = {
					age >= scope:secondary_actor.age_plus_10
					age <= scope:secondary_actor.age_minus_10
					scope:secondary_actor.age >= 30 # Concubines are meant to be young...
				}
			}
		}

		modifier = { # Incest Filtering: Hard NO!
			factor = 0
			scope:recipient = {
				relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:secondary_actor }
				NOR = {
					accepts_incest_with_trigger = { CHARACTER = scope:secondary_actor }
					sexually_liberal_trigger = yes
				}
			}
		}

		modifier = { # Incest Filtering: Not likely, but keep the door open...
			factor = 0.2 
			scope:recipient = {
				relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:secondary_actor }
				OR = {
					accepts_incest_with_trigger = { CHARACTER = scope:secondary_actor }
					sexually_liberal_trigger = yes
				}
			}
		}

		modifier = { # Faith considerations
			factor = 0
			scope:recipient = {
				faith = {
					faith_hostility_level = {
						target = scope:actor.faith
						value >= faith_hostile_level
					}
				}
				NOR = {
					has_relation_friend = scope:actor
					has_relation_best_friend = scope:actor
				}
			}
		}
	}

	on_send = {
		scope:secondary_actor = { # to block the same character from being offered twice
			add_character_flag = {
				flag = has_been_offered_as_concubine
				days = 5
			}
		}
	}
	
	on_accept = {
		concubine_offer_on_accept_effect = yes

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_gain
			DESC = clan_unity_concubinage_offer.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	on_decline = {
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = msg_concubine_offer_rejected_title
				right_icon = scope:recipient
				left_icon = scope:secondary_actor
				custom_tooltip = msg_concubine_offer_rejected
			}
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_loss
			DESC = clan_unity_concubinage_offer_decline.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	send_options_exclusive = no
	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = SCHEME_HOOK
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"
}

dismiss_concubine_interaction = {
	category = interaction_category_diplomacy
	use_diplomatic_range = no	
	icon = concubine_icon
	
	is_shown = {
		scope:recipient = {
			is_concubine_of = scope:actor
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = { NOT = { has_strong_hook = scope:actor } }
		scope:actor = {
			custom_tooltip = {
				NOT = {
					var:family_widow_forbidden_divorce ?= scope:target
				}
				text = BOUND_TO_FAMILY_WIDOW
			}
		}
	}

	auto_accept = yes
	
	on_accept = {
		scope:actor = {
			stress_impact = {
				compassionate = minor_stress_impact_gain
			}
			hidden_effect = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = msg_concubine_dismissed_title
					right_icon = scope:recipient
					remove_concubine = scope:recipient
				}
			}
		}
		
		scope:recipient = {
		# No point in sending a notification since the player can't be an unlanded concubine in any case
		#	trigger_event = marriage_interaction.0050

			# Being thrown aside is humiliating, even if you didn't want to be a concubine in the first place.
			add_opinion = {
				modifier = set_me_aside_opinion
				target = scope:actor
			}
			if = {
				limit = { scope:actor = { has_relation_lover = scope:recipient } }
				lover_breakup_effect = {
					BREAKER = scope:actor
					LOVER = scope:recipient
				}			
			}	

			# Figure out where we should go now that we're no longer a concubine.
			if = {
				limit = {
					is_councillor_of = scope:actor
				}
				# Then they should remain in court
			}
			else_if = { #Dismiss to relevant court, or to pool
				limit = {
					any_close_family_member = {
						is_landed_or_landless_administrative = yes
					}
				}
				random_close_family_member = {
					limit = {
						is_child_of = scope:recipient
						is_landed_or_landless_administrative = yes
					}
					alternative_limit = {
						is_parent_of = scope:recipient
						is_landed_or_landless_administrative = yes
					}
					alternative_limit = {
						is_landed_or_landless_administrative = yes
					}
					add_courtier = scope:recipient
				}
			}
			else = {
				move_to_pool = yes
			}
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_loss
			DESC = clan_unity_concubinage_dismissal.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
}

divorce_character_interaction = {
	category = interaction_category_diplomacy
	use_diplomatic_range = no
	icon = icon_marriage

	desc = divorce_character_interaction_desc
	cost = { piety = divorce_cost }

	is_shown = {
		scope:actor.faith = {
			# We are not required to seek approval of a spiritual HoF
			NAND = {
				has_doctrine = doctrine_divorce_approval
				has_doctrine = doctrine_spiritual_head
				exists = religious_head
			}
			# We are not required to seek approval of a house head
			NAND = {
				has_doctrine = doctrine_divorce_approval
				exists = scope:actor.house
				exists = scope:actor.house.house_head
				scope:actor = { is_house_head = no }
			}
		}
		scope:recipient = {
			is_spouse_of = scope:actor
			is_concubine = no # Can set aside Concubines at any time
		}
	}

	is_valid_showing_failures_only = {
		custom_tooltip = {
			NOT = {
				scope:actor.culture = {
					has_cultural_parameter = cannot_divorce
				}
			}
			text = CULTURE_FORBIDS_DIVORCE
		}
		NOT = {
			scope:actor.faith = {
				has_doctrine = doctrine_divorce_disallowed
			}
		}
		scope:recipient = { is_busy_in_events_localised = yes }
		scope:recipient = { NOT = { has_strong_hook = scope:actor } }
		scope:actor = {
			custom_tooltip = {
				NOT = {
					var:family_widow_forbidden_divorce ?= scope:target
				}
				text = BOUND_TO_FAMILY_WIDOW
			}
		}
	}

	auto_accept = yes

	on_accept = {
		scope:actor = {
			divorce_effect = {
				DIVORCER = scope:actor
				DIVORCEE = scope:recipient
			}
			if = {
				limit = {
					allowed_concubines = yes
				}
				custom_tooltip = divorce_cannot_concubine_ex_wife
			}
		}
		scope:recipient = {
			trigger_event = marriage_interaction.0030
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = major_unity_loss
			DESC = clan_unity_divorce.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	
	# AI
	ai_targets = {
		ai_recipients = spouses
	}
	ai_frequency = 36
	
	ai_potential = {
		is_ruler = yes
	}

	ai_will_do = {
		base = 0

		modifier = { # The AI wants to divorce should they not have an heir that is their own child, and their spouse is infertile
			add = 100
			scope:actor = {
				fertility > 0.1
				NOT = {
					any_child = {
						is_heir_of = scope:actor
					}
				}
			}
			scope:recipient = {
				is_ai = yes
				fertility < 0.1
			}
		}

		modifier = { # The AI will divorce if they're in a same-sex marriage and their faith changes its stance on the topic
			add = 100
			scope:actor = {
				sex_same_as = scope:recipient
				allowed_to_marry_same_sex_trigger = no
			}
		}
	}
}

divorce_character_house_head_interaction = {
	category = interaction_category_diplomacy
	use_diplomatic_range = no
	icon = icon_marriage

	cost = {
		piety = divorce_cost
	}

	redirect = {
		scope:recipient = {
			save_scope_as = secondary_recipient
		}
		if = {
			limit = {
				exists = scope:actor.house.house_head
			}
			scope:actor.house.house_head = {
				save_scope_as = recipient
			}
		}
	}

	greeting = positive
	notification_text = ASK_FOR_DIVORCE_NOTIFICATION

	is_shown = {
		exists = scope:actor.house.house_head
		NOT = { scope:actor = scope:actor.house.house_head } # no need to ask yourself
		scope:actor.faith = {
			has_doctrine = doctrine_divorce_approval
		}
		NAND = {
			exists = scope:actor.faith.religious_head
			scope:actor.faith = {
				has_doctrine = doctrine_spiritual_head
			}
		}
		scope:secondary_recipient = {
			is_spouse_of = scope:actor
			is_concubine = no # Can set aside Concubines at any time
		}
	}

	is_valid_showing_failures_only = {
		# Denounced trait?
		scope:recipient = { is_busy_in_events_localised = yes }
		scope:secondary_recipient = { is_busy_in_events_localised = yes }
		custom_tooltip = {
			NOT = {
				scope:actor.culture = {
					has_cultural_parameter = cannot_divorce
				}
			}
			text = CULTURE_FORBIDS_DIVORCE
		}
		NOT = {
			scope:secondary_recipient = { has_strong_hook = scope:actor }
		}
		scope:actor = {
			custom_tooltip = {
				NOT = {
					var:family_widow_forbidden_divorce ?= scope:target
				}
				text = BOUND_TO_FAMILY_WIDOW
			}
		}
	}
	
	ai_accept = {
		base = 0 # Should be 0 for all interactions
		
		divorce_character_ai_accept_modifier = yes
		
		modifier = { # Infertility
			add = 20
			desc = DIVORCE_INFERTILE
			scope:actor = {
				fertility > 0.1
			}
			scope:secondary_recipient = {
				fertility < 0.1
				is_eunuch_trigger = no
				NOT = {
					any_child = {
						is_child_of = scope:actor
					}
				}
			}
		}
		
		modifier = { # Negative for having given you children
			add = -50
			desc = DIVORCE_GIVEN_CHILDREN
			scope:secondary_recipient = {
				any_child = {
					count >= 3
					is_child_of = scope:actor
					AND = {
						exists = dynasty
						dynasty = scope:actor.dynasty
					}
				}
			}
		}
		
		modifier = { # They are of the same dynasty as the House Head
			trigger = { NOT = { scope:recipient = scope:secondary_recipient } }
			add = -10
			desc = THEY_ARE_MY_DYNASTY_MEMBER
			scope:recipient = {
				is_lowborn = no
				exists = dynasty
				NOR = {
					is_cousin_of = scope:secondary_recipient
					is_close_or_extended_family_of = scope:secondary_recipient
				}
				dynasty = scope:secondary_recipient.dynasty
				NOR = {
					house = scope:secondary_recipient.house
					is_close_or_extended_family_of = scope:actor
					is_cousin_of = scope:actor
					is_child_of = scope:actor
					AND = {
						exists = scope:actor.dynasty
						scope:recipient.dynasty = scope:actor.dynasty
					}
				}
			}
		}
		
		modifier = { # They are of the same house as the House Head
			trigger = { NOT = { scope:recipient = scope:secondary_recipient } }
			add = -20
			desc = THEY_ARE_MY_HOUSE_MEMBER
			scope:recipient = {
				is_lowborn = no
				NOR = {
					is_cousin_of = scope:secondary_recipient
					is_close_or_extended_family_of = scope:secondary_recipient
				}
				house = scope:secondary_recipient.house
				NOR = {
					house = scope:actor.house
					is_close_or_extended_family_of = scope:actor
					is_cousin_of = scope:actor
					is_child_of = scope:actor	
					scope:recipient.house = scope:actor.house
				}
			}
		}
		
		modifier = { # They are close kin to the House Head
			trigger = { NOT = { scope:recipient = scope:secondary_recipient } }
			add = -35
			desc = THEY_ARE_MY_CLOSE_KIN
			scope:recipient = {
				OR = {
					is_cousin_of = scope:secondary_recipient
					is_close_or_extended_family_of = scope:secondary_recipient
				}
			}
		}
		
		modifier = {
			add = intimidated_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		
		modifier = {
			add = intimidated_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}

		modifier = { # You're in a same-sex marriage and your faith doesn't allow it
			add = 100
			desc = SAME_SEX_MARRIAGE_FAITH_REASON_ACTOR
			scope:actor.faith = scope:recipient.faith
			scope:actor = {
				sex_same_as = scope:secondary_recipient
				allowed_to_marry_same_sex_trigger = no
			}
		}

		modifier = { # You're in a same-sex marriage and their faith doesn't allow it
			add = 100
			desc = SAME_SEX_MARRIAGE_FAITH_REASON
			scope:secondary_recipient.faith = scope:recipient.faith
			scope:secondary_recipient = {
				sex_same_as = scope:actor
				allowed_to_marry_same_sex_trigger = no
			}
		}

		# Unity modifiers
		evaluate_action_decreasing_house_unity = {
			VALUE = 100
		}
	}

	auto_accept = {
		custom_description = {
			text = "spending_hook"
			subject = scope:actor
			object = scope:recipient
			scope:divorce_hook = yes
		}
	}
	
	send_option = {
		is_shown = {
			NOT = { scope:actor = scope:recipient }
		}
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = divorce_hook
		localization = GENERIC_SPEND_A_HOOK
	}	
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"
	
	send_options_exclusive = no
	
	ai_min_reply_days = 1
	ai_max_reply_days = 5

	on_accept = {
		if = {
			limit = { always = scope:divorce_hook }
			scope:actor = {
				if = {
					limit = {
						has_usable_hook = scope:recipient
					}
					use_hook = scope:recipient
				}
			}
		}
		if = {
			limit = {
				scope:actor = scope:actor.house.house_head
			}
			scope:actor = {
				divorce_effect = {
					DIVORCER = scope:actor
					DIVORCEE = scope:secondary_recipient
				}
			}
		}
		else = {
			show_as_tooltip = {
				scope:actor = {
					divorce_effect = {
						DIVORCER = scope:actor
						DIVORCEE = scope:secondary_recipient
					}
				}
			}
			scope:actor = {
				trigger_event = religious_interaction.2
			}
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = major_unity_loss
			DESC = clan_unity_divorce_grant.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	on_decline = {
		send_interface_toast = {
			type = event_toast_effect_bad
			title = divorce_character_rel_head_interaction_decline_notification
			right_icon = scope:actor.house.house_head
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_loss
			DESC = clan_unity_divorce_grant_refusal.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	
	# AI
	ai_targets = {
		ai_recipients = spouses
	}
	ai_frequency = 36
	
	ai_potential = {
		is_ruler = yes
	}

	ai_will_do = {
		base = 0

		modifier = { # The AI wants to divorce should they not have an heir that is their own child, and their spouse is infertile
			add = 100
			scope:actor = {
				fertility > 0.1
				NOT = {
					any_child = {
						is_heir_of = scope:actor
					}
				}
			}
			scope:secondary_recipient = {
				is_ai = yes
				fertility < 0.1
			}
		}
		
		modifier = { # The AI will only use a Hook if they couldn't otherwise do this
			scope:divorce_hook = yes
			add = -1
		}

		modifier = { # You're in a same-sex marriage and your faith doesn't allow it
			add = 100
			scope:actor = {
				sex_same_as = scope:secondary_recipient
				allowed_to_marry_same_sex_trigger = no
			}
		}
	}
}

divorce_character_rel_head_interaction = {
	category = interaction_category_diplomacy
	use_diplomatic_range = no
	icon = icon_marriage

	cost = {
		piety = divorce_cost
	}

	redirect = {
		scope:recipient = {
			save_scope_as = secondary_recipient
		}

		if = {
			limit = {
				exists = scope:actor.faith.religious_head
			}
			scope:actor.faith.religious_head = {
				save_scope_as = recipient
			}
		}
	}

	greeting = positive
	notification_text = ASK_FOR_DIVORCE_NOTIFICATION

	is_shown = {
		exists = scope:actor.faith.religious_head
		scope:actor.faith = {
			has_doctrine = doctrine_spiritual_head
			has_doctrine = doctrine_divorce_approval
		}
		scope:secondary_recipient = {
			is_spouse_of = scope:actor
			is_concubine = no # Can set aside Concubines at any time
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			NOT = { has_trait = excommunicated }
		}
		scope:recipient = { is_busy_in_events_localised = yes }
		scope:secondary_recipient = { is_busy_in_events_localised = yes }
		custom_tooltip = {
			NOT = {
				scope:actor.culture = {
					has_cultural_parameter = cannot_divorce
				}
			}
			text = CULTURE_FORBIDS_DIVORCE
		}
		NOT = {
			scope:secondary_recipient = { has_strong_hook = scope:actor }
		}
		scope:actor = {
			custom_tooltip = {
				NOT = {
					var:family_widow_forbidden_divorce ?= scope:recipient
				}
				text = BOUND_TO_FAMILY_WIDOW
			}
		}
	}

	ai_accept = {
		base = 0 # Should be 0 for all interactions
		
		divorce_character_ai_accept_modifier = yes

		modifier = { # Divine Family is part of the faith
			add = -100
			desc = DIVORCE_INCESTUOUS

			scope:actor = { 
				is_close_or_extended_family_of = scope:secondary_recipient
				faith = { has_doctrine = tenet_divine_marriage }
			}
		}
		
		modifier = { # More likely if the marriage is incestuous, unless Divine Family is part of the faith
			add = 100
			desc = DIVORCE_INCESTUOUS

			scope:actor = {
				relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:secondary_recipient	}
			}
		}		
		
		modifier = { # They are of the same dynasty as the Head of Faith
			add = -5
			desc = THEY_ARE_MY_DYNASTY_MEMBER
			scope:recipient = {
				is_lowborn = no
				exists = dynasty
				exists = scope:secondary_recipient.dynasty
				dynasty = scope:secondary_recipient.dynasty
				NOR = {
					house = scope:secondary_recipient.house
					is_close_or_extended_family_of = scope:actor
					is_cousin_of = scope:actor
					is_child_of = scope:actor	
					scope:recipient.dynasty = scope:actor.dynasty
				}
			}
		}
		
		modifier = { # You are of the same dynasty as the Head of Faith
			add = 5
			desc = YOU_ARE_MY_DYNASTY_MEMBER
			scope:recipient = {
				is_lowborn = no
				exists = dynasty
				exists = scope:actor.dynasty
				dynasty = scope:actor.dynasty
				NOR = {
					is_close_or_extended_family_of = scope:actor
					is_cousin_of = scope:actor
					is_child_of = scope:actor
					scope:recipient.dynasty = scope:secondary_recipient.dynasty
				}
			}
		}
		
		modifier = { # They are of the same house as the Head of Faith
			add = -10
			desc = THEY_ARE_MY_HOUSE_MEMBER
			scope:recipient = {
				is_lowborn = no
				exists = house
				exists = scope:secondary_recipient.house
				house = scope:secondary_recipient.house
				NOR = {
					house = scope:actor.house
					is_close_or_extended_family_of = scope:actor
					is_cousin_of = scope:actor
					is_child_of = scope:actor	
					scope:recipient.house = scope:actor.house
				}
			}
		}
		
		modifier = { # You are of the same house as the Head of Faith
			add = 10
			desc = YOU_ARE_MY_HOUSE_MEMBER
			scope:recipient = {
				is_lowborn = no
				exists = house
				exists = scope:actor.house
				house = scope:actor.house
				NOR = {
					is_close_or_extended_family_of = scope:actor
					is_cousin_of = scope:actor
					is_child_of = scope:actor
					scope:recipient.house = scope:secondary_recipient.house
				}
			}
		}
		
		modifier = { # Related to the Head of Faith
			add = 25
			desc = YOU_ARE_MY_RELATIVE
			scope:recipient = {
				OR = {
					is_close_or_extended_family_of = scope:actor
					is_cousin_of = scope:actor
				}
				NOT = { is_child_of = scope:actor }
			}
		}
		
		modifier = { # Parent of the Head of Faith
			add = 50
			desc = PARENTS #YOU_ARE_MY_PARENT
			scope:recipient = {
				is_child_of = scope:actor
			}
		}
		
		modifier = {
			add = {
				add = 10
				multiply = scope:actor.num_virtuous_traits
			}
			scope:actor.num_virtuous_traits > 0
			desc = I_AM_VIRTUOUS
		}
		
		modifier = {
			add = {
				add = -10
				multiply = scope:secondary_recipient.num_virtuous_traits
			}
			scope:secondary_recipient.num_virtuous_traits > 0
			desc = THEY_ARE_VIRTUOUS
		}
		
		modifier = {
			add = {
				add = -10
				multiply = scope:actor.num_sinful_traits
			}
			scope:actor.num_sinful_traits > 0
			desc = I_AM_SINFUL
		}
		
		modifier = {
			add = {
				add = 10
				multiply = scope:secondary_recipient.num_sinful_traits
			}
			scope:secondary_recipient.num_sinful_traits > 0
			desc = THEY_ARE_SINFUL
		}

		modifier = { # You're in a same-sex marriage and your faith doesn't allow it
			add = 100
			desc = SAME_SEX_MARRIAGE_FAITH_REASON_ACTOR
			scope:actor.faith = scope:recipient.faith
			scope:actor = {
				sex_same_as = scope:secondary_recipient
				allowed_to_marry_same_sex_trigger = no
			}
		}

		modifier = { # You're in a same-sex marriage and their faith doesn't allow it
			add = 100
			desc = SAME_SEX_MARRIAGE_FAITH_REASON
			scope:secondary_recipient.faith = scope:recipient.faith
			scope:secondary_recipient = {
				sex_same_as = scope:actor
				allowed_to_marry_same_sex_trigger = no
			}
		}

		modifier = { # You have denounced them as impotent (ep2_wedding.1061)
			add = 30
			desc = THEY_ARE_ACCUSED_IMPOTENCY
			scope:secondary_recipient = {
				has_character_modifier = wedding_accused_impotency_modifier
			}
		}
		# Unity modifiers
		evaluate_action_decreasing_house_unity = {
			VALUE = 100
		}
	}
	
	auto_accept = {
		custom_description = {
			text = "spending_hook"
			subject = scope:actor
			object = scope:recipient
			scope:divorce_hook = yes
		}
	}
	
	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = divorce_hook
		localization = GENERIC_SPEND_A_HOOK
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"
	
	send_options_exclusive = no
	
	ai_min_reply_days = 1
	ai_max_reply_days = 5

	on_accept = {
		if = {
			limit = { always = scope:divorce_hook }
			scope:actor = {
				if = {
					limit = {
						has_usable_hook = scope:recipient
					}
					use_hook = scope:recipient
				}
			}
		}
		show_as_tooltip = {
			scope:actor = {
				divorce_effect = {
					DIVORCER = scope:actor
					DIVORCEE = scope:secondary_recipient
				}
			}
		}
		scope:actor = {
			trigger_event = religious_interaction.1
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = major_unity_loss
			DESC = clan_unity_alliance.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	on_decline = {
		send_interface_toast = {
			type = event_toast_effect_bad
			title = divorce_character_rel_head_interaction_decline_notification

			right_icon = scope:actor.faith.religious_head
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_loss
			DESC = clan_unity_alliance.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	
	# AI
	ai_targets = {
		ai_recipients = spouses
	}
	ai_frequency = 36
	
	ai_potential = {
		is_ruler = yes
	}

	ai_will_do = {
		base = 0

		modifier = { # The AI wants to divorce should they not have an heir that is their own child, and their spouse is infertile
			add = 100
			scope:actor = {
				fertility > 0.1
				NOT = {
					any_child = {
						is_heir_of = scope:actor
					}
				}
			}
			scope:secondary_recipient = {
				is_ai = yes
				fertility < 0.1
			}
		}
		
		modifier = { # The AI will only use a Hook if they couldn't otherwise do this
			scope:divorce_hook = yes
			add = -1
		}

		modifier = { # You're in a same-sex marriage and your faith doesn't allow it
			add = 100
			scope:actor = {
				sex_same_as = scope:secondary_recipient
				allowed_to_marry_same_sex_trigger = no
			}
		}
	}	
}

divorce_character_dynast_request_interaction = {
	category = interaction_category_diplomacy
	use_diplomatic_range = no
	icon = icon_dynasty

	desc = divorce_character_dynast_request_interaction_desc

	cost = {
		piety = divorce_cost
		renown = minor_dynasty_prestige_value
	}

	redirect = {
		scope:recipient = {
			save_scope_as = secondary_actor
		}

		if = {
			limit = {
				exists = scope:recipient.primary_spouse
			}
			scope:recipient.primary_spouse = {
				save_scope_as = recipient
			}
		}
	}

	is_shown = {
		NOT = { scope:secondary_actor = scope:actor }
		scope:secondary_actor.dynasty.dynast = scope:actor

		scope:actor.faith = {
			# We are not required to seek approval of a spiritual HoF
			NAND = {
				has_doctrine = doctrine_divorce_approval
				has_doctrine = doctrine_spiritual_head
				exists = religious_head
			}
		}
		OR = {
			AND = {
				NOT = { scope:actor.faith = scope:secondary_actor.faith }
				# They are not required to seek approval of a spiritual HoF of the Dynasty Member
				scope:secondary_actor.faith = {
					NOT = {
						has_doctrine = doctrine_divorce_approval
					}
				}
			}
			scope:actor.faith = scope:secondary_actor.faith
		}
		
		scope:recipient = {
			is_spouse_of = scope:secondary_actor
			is_concubine = no # Can set aside Concubines at any time
		}
	}

	is_valid_showing_failures_only = {
		custom_tooltip = {
			NOT = {
				scope:actor.culture = {
					has_cultural_parameter = cannot_divorce
				}
			}
			text = CULTURE_FORBIDS_DIVORCE
		}
		NOT = {
			scope:actor.faith = {
				has_doctrine = doctrine_divorce_disallowed
			}
		}
		scope:actor.dynasty = {
			dynasty_prestige >= minor_dynasty_prestige_value
		}

		NOT = {
			scope:secondary_actor.faith = {
				has_doctrine = doctrine_divorce_disallowed
			}
		}
		scope:secondary_actor = { is_busy_in_events_localised = yes }
		scope:secondary_actor = { NOT = { has_strong_hook = scope:actor } }
		trigger_if = {
			limit = {
				scope:secondary_actor = {
					NOR = {
						is_child_of = scope:actor
						is_grandchild_of = scope:actor
						is_great_grandchild_of = scope:actor
					}
				}
			}
			custom_description = {
				scope:secondary_actor.top_liege = scope:actor.top_liege
				text = "same_realm_as"
			}
		}
		scope:actor = {
			custom_tooltip = {
				NOT = {
					var:family_widow_forbidden_divorce ?= scope:target
				}
				text = BOUND_TO_FAMILY_WIDOW
			}
		}
	}

	auto_accept = yes

	on_accept = {
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = divorce_character_dynast_request_interaction_title
				custom_tooltip = divorce_character_dynast_request_interaction_desc

				left_icon = scope:secondary_actor
				right_icon = scope:recipient
			}
		}


		scope:secondary_actor = {
			divorce_effect = {
				DIVORCER = scope:secondary_actor
				DIVORCEE = scope:recipient
			}

			add_opinion = {
				modifier = forced_divorce_opinion
				target = scope:actor
			}
		}
		if = {
			limit = {
				NOT = { scope:recipient = scope:actor }
			}
			scope:recipient = {
				trigger_event = marriage_interaction.0030

				add_opinion = {
					modifier = forced_divorce_opinion
					target = scope:actor
				}
			}
		}
	}
	
	# AI
	ai_targets = {
		ai_recipients = dynasty
		max = 10
	}
	ai_frequency = 72
	
	ai_potential = {
		is_ruler = yes
	}

	ai_will_do = {
		base = 0

		modifier = { # The AI wants to force a divorce if their heir is not from their dynasty and current marriage within the dynasty are not promising
			add = 100
			scope:secondary_actor = {
				fertility > 0.1
			}
			scope:recipient = {
				is_ai = yes
				fertility < 0.1
			}
			exists = scope:actor.primary_heir
			exists = scope:actor.primary_heir.dynasty
			NOT = { scope:actor.primary_heir.dynasty = this.dynasty }
		}

		#Players should not be targeted 
		modifier = {
			add = -1000
			OR = {
				scope:secondary_actor = { is_ai = no }
				scope:recipient = { is_ai = no }
			}
		}
	}
}

divorce_character_dynast_request_rel_head_interaction = {
	category = interaction_category_diplomacy
	use_diplomatic_range = no
	icon = icon_dynasty

	cost = {
		piety = divorce_cost
		renown = minor_dynasty_prestige_value
	}

	redirect = {
		scope:recipient = {
			save_scope_as = secondary_actor
		}

		if = {
			limit = {
				exists = scope:recipient.primary_spouse
			}
			scope:recipient.primary_spouse = {
				save_scope_as = secondary_recipient
			}
		}
		
		if = {
			limit = {
				exists = scope:actor.faith.religious_head
			}
			scope:actor.faith.religious_head = {
				save_scope_as = recipient
			}
		}
	}

	greeting = positive
	notification_text = ASK_FOR_DIVORCE_NOTIFICATION

	is_shown = {
		NOT = { scope:secondary_actor = scope:actor }
		scope:secondary_actor.dynasty.dynast = scope:actor
		NOT = {
			scope:secondary_actor = { is_spouse_of = scope:actor }
		}

		exists = scope:secondary_recipient

		scope:actor.faith = scope:secondary_actor.faith
		exists = scope:actor.faith.religious_head
		scope:actor.faith = {
			has_doctrine = doctrine_spiritual_head
			has_doctrine = doctrine_divorce_approval
		}
	}

	is_valid_showing_failures_only = {
		custom_tooltip = {
			NOT = {
				scope:actor.culture = {
					has_cultural_parameter = cannot_divorce
				}
			}
			text = CULTURE_FORBIDS_DIVORCE
		}
		scope:actor.dynasty = {
			dynasty_prestige >= minor_dynasty_prestige_value
		}

		scope:secondary_actor = { is_busy_in_events_localised = yes }
		scope:secondary_actor = { NOT = { has_strong_hook = scope:actor } }
		trigger_if = {
			limit = {
				scope:secondary_actor = {
					NOR = {
						is_child_of = scope:actor
						is_grandchild_of = scope:actor
						is_great_grandchild_of = scope:actor
					}
				}
			}
			custom_description = {
				scope:secondary_actor.top_liege = scope:actor.top_liege
				text = "same_realm_as"
			}
		}
		scope:actor = {
			custom_tooltip = {
				NOT = {
					var:family_widow_forbidden_divorce ?= scope:target
				}
				text = BOUND_TO_FAMILY_WIDOW
			}
		}
	}

	ai_accept = {
		base = 0 # Should be 0 for all interactions
		
		divorce_character_ai_accept_modifier = yes

		modifier = { # Divine Family is part of the faith
			add = -100
			desc = DIVORCE_INCESTUOUS

			scope:secondary_actor = { 
				is_close_or_extended_family_of = scope:secondary_recipient
				faith = { has_doctrine = tenet_divine_marriage }
			}
		}
		
		modifier = { # More likely if the marriage is incestuous, unless Divine Family is part of the faith
			add = 100
			desc = DIVORCE_INCESTUOUS

			scope:secondary_actor = {
				relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:secondary_recipient	}
			}
		}		
		
		modifier = { # You are of the same dynasty as the Head of Faith
			add = 5
			desc = YOU_ARE_MY_DYNASTY_MEMBER
			scope:recipient = {
				is_lowborn = no
				dynasty = scope:actor.dynasty
				NOR = {
					is_close_or_extended_family_of = scope:actor
					is_cousin_of = scope:actor
					is_child_of = scope:actor
					scope:recipient.dynasty = scope:secondary_recipient.dynasty
				}
			}
		}
		
		
		modifier = { # You are of the same house as the Head of Faith
			add = 10
			desc = YOU_ARE_MY_HOUSE_MEMBER
			scope:recipient = {
				is_lowborn = no
				house = scope:actor.house
				NOR = {
					is_close_or_extended_family_of = scope:actor
					is_cousin_of = scope:actor
					is_child_of = scope:actor
					scope:recipient.house = scope:secondary_recipient.house
				}
			}
		}
		
		modifier = { # Related to the Head of Faith
			add = 25
			desc = YOU_ARE_MY_RELATIVE
			scope:recipient = {
				OR = {
					is_close_or_extended_family_of = scope:actor
					is_cousin_of = scope:actor
				}
				NOT = { is_child_of = scope:actor }
			}
		}
		
		modifier = { # Parent of the Head of Faith
			add = 50
			desc = PARENTS #YOU_ARE_MY_PARENT
			scope:recipient = {
				is_child_of = scope:actor
			}
		}
		
		modifier = {
			add = {
				add = 10
				multiply = scope:actor.num_virtuous_traits
			}
			scope:actor.num_virtuous_traits > 0
			desc = I_AM_VIRTUOUS
		}
		
		modifier = {
			add = {
				add = -10
				multiply = scope:secondary_recipient.num_virtuous_traits
			}
			scope:secondary_recipient.num_virtuous_traits > 0
			desc = THEY_ARE_VIRTUOUS
		}
		
		modifier = {
			add = {
				add = -10
				multiply = scope:actor.num_sinful_traits
			}
			scope:actor.num_sinful_traits > 0
			desc = I_AM_SINFUL
		}
		
		modifier = {
			add = {
				add = 10
				multiply = scope:secondary_recipient.num_sinful_traits
			}
			scope:secondary_recipient.num_sinful_traits > 0
			desc = THEY_ARE_SINFUL
		}
	}
	
	auto_accept = {
		custom_description = {
			text = "spending_hook"
			subject = scope:actor
			object = scope:recipient
			scope:divorce_hook = yes
		}
	}
	
	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = divorce_hook
		localization = GENERIC_SPEND_A_HOOK
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"
	
	send_options_exclusive = no
	
	ai_min_reply_days = 1
	ai_max_reply_days = 5

	on_accept = {
		if = {
			limit = { always = scope:divorce_hook }
			scope:actor = {
				if = {
					limit = {
						has_usable_hook = scope:recipient
					}
					use_hook = scope:recipient
				}
			}
		}
		scope:actor = {
			trigger_event = religious_interaction.6
		}

		scope:secondary_actor = {
			divorce_effect = {
				DIVORCER = scope:secondary_actor
				DIVORCEE = scope:secondary_recipient
			}
			add_opinion = {
				modifier = forced_divorce_opinion
				target = scope:actor
			}
		}

		scope:secondary_recipient = {
			add_opinion = {
				modifier = forced_divorce_opinion
				target = scope:actor
			}
		}
	}

	on_decline = {
		send_interface_toast = {
			type = event_toast_effect_bad
			title = divorce_character_dynast_request_rel_head_interaction_decline_notification

			right_icon = scope:actor.faith.religious_head
		}
	}
	
	# AI
	ai_targets = {
		ai_recipients = dynasty
		max = 10
	}
	ai_frequency = 72
	
	ai_potential = {
		is_ruler = yes
	}

	ai_will_do = {
		base = 0

		modifier = { # The AI wants to force a divorce if their heir is not from their dynasty and current marriage within the dynasty are not promising
			add = 100
			scope:secondary_actor = {
				fertility > 0.1
			}
			scope:secondary_recipient = {
				is_ai = yes
				fertility < 0.1
			}
			exists = scope:actor.primary_heir
			exists = scope:actor.primary_heir.dynasty
			NOT = { scope:actor.primary_heir.dynasty = this.dynasty }
		}
		
		modifier = { # The AI will only use a Hook if they couldn't otherwise do this
			scope:divorce_hook = yes
			add = -1
		}

		#Players should not be targeted 
		modifier = {
			add = -1000
			OR = {
				scope:secondary_actor = { is_ai = no }
				scope:secondary_recipient = { is_ai = no }
			}
		}
	}	
}

elope_interaction = {
	icon = icon_scheme_elope
	category = interaction_category_friendly

	scheme = elope

	desc = elope_interaction_desc

	notification_text = elope_interaction_notification
	
	ai_targets = {
		ai_recipients = realm_characters
		chance = 0.25
	}
	
	ai_target_quick_trigger = {
		adult = yes
		attracted_to_owner = yes
		owner_attracted = yes
	}
	ai_frequency = 72

	redirect = {
		scope:recipient = {
			save_scope_as = secondary_recipient
		}
	}
	
	is_shown = {
		scope:actor = {
			OR = {
				has_relation_lover = scope:recipient
				has_relation_soulmate = scope:recipient
				AND = {
					is_landless_adventurer = yes
					has_perk = accomplished_forger_perk
				}
			}
		}
		scope:recipient = {
			is_adult = yes
			is_courtier = yes
			NOR = {
				is_courtier_of = scope:actor
				is_consort_of = scope:actor
			}
			could_marry_character_trigger = { CHARACTER = scope:actor }
		}
	}
	

	is_valid_showing_failures_only = {
		scope:recipient = { is_busy_in_events_localised = yes }
		scope:actor = {
			can_start_scheme = {
				type = elope
				target_character = scope:recipient
			}
		}
	}

	
	ai_accept = {
		base = 0

		elopment_ai_accept_modifier = yes
	}
	
	ai_potential = {
		is_adult = yes
		OR = {
			num_of_relation_lover > 0
			num_of_relation_soulmate > 0
		}
		NOR = {
			has_trait = celibate
			scheme_generic_ai_blocker_trigger = yes
		}
	}
	
	ai_min_reply_days = 1
	ai_max_reply_days = 3
	
	on_accept = {
		scope:actor = {
			stress_impact = {
				honest = minor_stress_impact_gain
				just = minor_stress_impact_gain
			}
			show_as_tooltip = {
				elope_success_effect = {
					TARGET = scope:recipient
					OWNER = scope:actor
				}
			}
		}
		hidden_effect = {
			scope:actor = {
				send_interface_toast = {
					type = event_toast_effect_good
					title = elope_interaction_notification

					left_icon = scope:actor					
					right_icon = scope:recipient

					begin_scheme_basic_effect = {
						SCHEME_TYPE = elope
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
					}

					show_as_tooltip = {
						stress_impact = {
							honest = minor_stress_impact_gain
							just = minor_stress_impact_gain
						}
					}
				}
			}
		}
	}

	on_decline = {
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = elope_interaction_decline_toast
				left_icon = scope:recipient
			}
		}
	}
	
	ai_will_do = {
		base = 100
	}
}
